5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
6 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <sys/types.h>
37 #include <dbus/dbus.h>
40 #define CONNMAN_API_SUBJECT_TO_CHANGE
41 #include <connman/plugin.h>
42 #include <connman/provider.h>
43 #include <connman/log.h>
44 #include <connman/task.h>
45 #include <connman/dbus.h>
46 #include <connman/inet.h>
47 #include <connman/agent.h>
48 #include <connman/setting.h>
49 #include <connman/vpn-dbus.h>
51 #include "../vpn-provider.h"
52 #include "../vpn-agent.h"
56 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
74 const char *vpn_default;
77 { "L2TP.User", "name", OPT_ALL, NULL, OPT_STRING },
78 { "L2TP.BPS", "bps", OPT_L2, NULL, OPT_STRING },
79 { "L2TP.TXBPS", "tx bps", OPT_L2, NULL, OPT_STRING },
80 { "L2TP.RXBPS", "rx bps", OPT_L2, NULL, OPT_STRING },
81 { "L2TP.LengthBit", "length bit", OPT_L2, NULL, OPT_STRING },
82 { "L2TP.Challenge", "challenge", OPT_L2, NULL, OPT_STRING },
83 { "L2TP.DefaultRoute", "defaultroute", OPT_L2, NULL, OPT_STRING },
84 { "L2TP.FlowBit", "flow bit", OPT_L2, NULL, OPT_STRING },
85 { "L2TP.TunnelRWS", "tunnel rws", OPT_L2, NULL, OPT_STRING },
86 { "L2TP.Exclusive", "exclusive", OPT_L2, NULL, OPT_STRING },
87 { "L2TP.Autodial", "autodial", OPT_L2, "yes", OPT_STRING },
88 { "L2TP.Redial", "redial", OPT_L2, "yes", OPT_STRING },
89 { "L2TP.RedialTimeout", "redial timeout", OPT_L2, "10", OPT_STRING },
90 { "L2TP.MaxRedials", "max redials", OPT_L2, NULL, OPT_STRING },
91 { "L2TP.RequirePAP", "require pap", OPT_L2, "no", OPT_STRING },
92 { "L2TP.RequireCHAP", "require chap", OPT_L2, "yes", OPT_STRING },
93 { "L2TP.ReqAuth", "require authentication", OPT_L2, "no", OPT_STRING },
94 { "L2TP.AccessControl", "access control", OPT_L2G, "yes", OPT_STRING },
95 { "L2TP.AuthFile", "auth file", OPT_L2G, NULL, OPT_STRING },
96 { "L2TP.ForceUserSpace", "force userspace", OPT_L2G, NULL, OPT_STRING },
97 { "L2TP.ListenAddr", "listen-addr", OPT_L2G, NULL, OPT_STRING },
98 { "L2TP.Rand Source", "rand source", OPT_L2G, NULL, OPT_STRING },
99 { "L2TP.IPsecSaref", "ipsec saref", OPT_L2G, NULL, OPT_STRING },
100 { "L2TP.Port", "port", OPT_L2G, NULL, OPT_STRING },
101 { "PPPD.EchoFailure", "lcp-echo-failure", OPT_PPPD, "0", OPT_STRING },
102 { "PPPD.EchoInterval", "lcp-echo-interval", OPT_PPPD, "0", OPT_STRING },
103 { "PPPD.Debug", "debug", OPT_PPPD, NULL, OPT_STRING },
104 { "PPPD.RefuseEAP", "refuse-eap", OPT_PPPD, NULL, OPT_BOOL },
105 { "PPPD.RefusePAP", "refuse-pap", OPT_PPPD, NULL, OPT_BOOL },
106 { "PPPD.RefuseCHAP", "refuse-chap", OPT_PPPD, NULL, OPT_BOOL },
107 { "PPPD.RefuseMSCHAP", "refuse-mschap", OPT_PPPD, NULL, OPT_BOOL },
108 { "PPPD.RefuseMSCHAP2", "refuse-mschapv2", OPT_PPPD, NULL, OPT_BOOL },
109 { "PPPD.NoBSDComp", "nobsdcomp", OPT_PPPD, NULL, OPT_BOOL },
110 { "PPPD.NoPcomp", "nopcomp", OPT_PPPD, NULL, OPT_BOOL },
111 { "PPPD.UseAccomp", "accomp", OPT_PPPD, NULL, OPT_BOOL },
112 { "PPPD.NoDeflate", "nodeflate", OPT_PPPD, NULL, OPT_BOOL },
113 { "PPPD.ReqMPPE", "require-mppe", OPT_PPPD, NULL, OPT_BOOL },
114 { "PPPD.ReqMPPE40", "require-mppe-40", OPT_PPPD, NULL, OPT_BOOL },
115 { "PPPD.ReqMPPE128", "require-mppe-128", OPT_PPPD, NULL, OPT_BOOL },
116 { "PPPD.ReqMPPEStateful", "mppe-stateful", OPT_PPPD, NULL, OPT_BOOL },
117 { "PPPD.NoVJ", "no-vj-comp", OPT_PPPD, NULL, OPT_BOOL },
120 static DBusConnection *connection;
122 struct l2tp_private_data {
123 struct connman_task *task;
125 vpn_provider_connect_cb_t cb;
129 static DBusMessage *l2tp_get_sec(struct connman_task *task,
130 DBusMessage *msg, void *user_data)
132 const char *user, *passwd;
133 struct vpn_provider *provider = user_data;
135 if (dbus_message_get_no_reply(msg) == FALSE) {
138 user = vpn_provider_get_string(provider, "L2TP.User");
139 passwd = vpn_provider_get_string(provider, "L2TP.Password");
141 if (user == NULL || strlen(user) == 0 ||
142 passwd == NULL || strlen(passwd) == 0)
145 reply = dbus_message_new_method_return(msg);
149 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
150 DBUS_TYPE_STRING, &passwd,
159 static int l2tp_notify(DBusMessage *msg, struct vpn_provider *provider)
161 DBusMessageIter iter, dict;
162 const char *reason, *key, *value;
163 char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
164 char *ifname = NULL, *nameservers = NULL;
165 struct connman_ipaddress *ipaddress = NULL;
167 dbus_message_iter_init(msg, &iter);
169 dbus_message_iter_get_basic(&iter, &reason);
170 dbus_message_iter_next(&iter);
173 connman_error("No provider found");
174 return VPN_STATE_FAILURE;
177 if (strcmp(reason, "auth failed") == 0) {
178 DBG("authentication failure");
180 vpn_provider_set_string(provider, "L2TP.User", NULL);
181 vpn_provider_set_string(provider, "L2TP.Password", NULL);
183 return VPN_STATE_AUTH_FAILURE;
186 if (strcmp(reason, "connect"))
187 return VPN_STATE_DISCONNECT;
189 dbus_message_iter_recurse(&iter, &dict);
191 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
192 DBusMessageIter entry;
194 dbus_message_iter_recurse(&dict, &entry);
195 dbus_message_iter_get_basic(&entry, &key);
196 dbus_message_iter_next(&entry);
197 dbus_message_iter_get_basic(&entry, &value);
199 DBG("%s = %s", key, value);
201 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
202 vpn_provider_set_string(provider, "Address", value);
203 addressv4 = g_strdup(value);
206 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
207 vpn_provider_set_string(provider, "Netmask", value);
208 netmask = g_strdup(value);
211 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
212 vpn_provider_set_string(provider, "DNS", value);
213 nameservers = g_strdup(value);
216 if (!strcmp(key, "INTERNAL_IFNAME"))
217 ifname = g_strdup(value);
219 dbus_message_iter_next(&dict);
222 if (vpn_set_ifname(provider, ifname) < 0) {
227 return VPN_STATE_FAILURE;
230 if (addressv4 != NULL)
231 ipaddress = connman_ipaddress_alloc(AF_INET);
235 if (ipaddress == NULL) {
236 connman_error("No IP address for provider");
240 return VPN_STATE_FAILURE;
243 value = vpn_provider_get_string(provider, "HostIP");
245 vpn_provider_set_string(provider, "Gateway", value);
246 gateway = g_strdup(value);
249 if (addressv4 != NULL)
250 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
253 vpn_provider_set_ipaddress(provider, ipaddress);
254 vpn_provider_set_nameservers(provider, nameservers);
260 connman_ipaddress_free(ipaddress);
262 return VPN_STATE_CONNECT;
265 static int l2tp_save(struct vpn_provider *provider, GKeyFile *keyfile)
268 connman_bool_t l2tp_option, pppd_option;
271 for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
272 l2tp_option = pppd_option = FALSE;
274 if (strncmp(pppd_options[i].cm_opt, "L2TP.", 5) == 0)
277 if (strncmp(pppd_options[i].cm_opt, "PPPD.", 5) == 0)
280 if (l2tp_option == TRUE || pppd_option == TRUE) {
281 option = vpn_provider_get_string(provider,
282 pppd_options[i].cm_opt);
283 if (option == NULL) {
285 * Check if the option prefix is L2TP as the
286 * PPPD options were using L2TP prefix earlier.
290 if (pppd_option == FALSE)
293 l2tp_str = g_strdup_printf("L2TP.%s",
294 &pppd_options[i].cm_opt[5]);
295 option = vpn_provider_get_string(provider,
303 g_key_file_set_string(keyfile,
304 vpn_provider_get_save_group(provider),
305 pppd_options[i].cm_opt, option);
312 static ssize_t full_write(int fd, const void *buf, size_t len)
317 byte_write = write(fd, buf, len);
318 if (byte_write < 0) {
319 connman_error("failed to write config to l2tp: %s\n",
330 static ssize_t l2tp_write_bool_option(int fd,
331 const char *key, const char *value)
336 if (key != NULL && value != NULL) {
337 if (strcasecmp(value, "yes") == 0 ||
338 strcasecmp(value, "true") == 0 ||
339 strcmp(value, "1") == 0) {
340 buf = g_strdup_printf("%s\n", key);
341 ret = full_write(fd, buf, strlen(buf));
350 static int l2tp_write_option(int fd, const char *key, const char *value)
357 buf = g_strdup_printf("%s %s\n", key, value);
359 buf = g_strdup_printf("%s\n", key);
361 ret = full_write(fd, buf, strlen(buf));
369 static int l2tp_write_section(int fd, const char *key, const char *value)
374 if (key != NULL && value != NULL) {
375 buf = g_strdup_printf("%s = %s\n", key, value);
376 ret = full_write(fd, buf, strlen(buf));
384 static int write_pppd_option(struct vpn_provider *provider, int fd)
389 l2tp_write_option(fd, "nodetach", NULL);
390 l2tp_write_option(fd, "lock", NULL);
391 l2tp_write_option(fd, "usepeerdns", NULL);
392 l2tp_write_option(fd, "noipdefault", NULL);
393 l2tp_write_option(fd, "noauth", NULL);
394 l2tp_write_option(fd, "nodefaultroute", NULL);
395 l2tp_write_option(fd, "ipparam", "l2tp_plugin");
397 for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
398 if (pppd_options[i].sub != OPT_ALL &&
399 pppd_options[i].sub != OPT_PPPD)
402 opt_s = vpn_provider_get_string(provider,
403 pppd_options[i].cm_opt);
405 opt_s = pppd_options[i].vpn_default;
410 if (pppd_options[i].type == OPT_STRING)
411 l2tp_write_option(fd,
412 pppd_options[i].pppd_opt, opt_s);
413 else if (pppd_options[i].type == OPT_BOOL)
414 l2tp_write_bool_option(fd,
415 pppd_options[i].pppd_opt, opt_s);
418 l2tp_write_option(fd, "plugin",
419 SCRIPTDIR "/libppp-plugin.so");
425 static int l2tp_write_fields(struct vpn_provider *provider,
431 for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
432 if (pppd_options[i].sub != sub)
435 opt_s = vpn_provider_get_string(provider,
436 pppd_options[i].cm_opt);
438 opt_s = pppd_options[i].vpn_default;
443 if (pppd_options[i].type == OPT_STRING)
444 l2tp_write_section(fd,
445 pppd_options[i].pppd_opt, opt_s);
446 else if (pppd_options[i].type == OPT_BOOL)
447 l2tp_write_bool_option(fd,
448 pppd_options[i].pppd_opt, opt_s);
454 static int l2tp_write_config(struct vpn_provider *provider,
455 const char *pppd_name, int fd)
459 l2tp_write_option(fd, "[global]", NULL);
460 l2tp_write_fields(provider, fd, OPT_L2G);
462 l2tp_write_option(fd, "[lac l2tp]", NULL);
464 option = vpn_provider_get_string(provider, "Host");
465 l2tp_write_option(fd, "lns =", option);
467 l2tp_write_fields(provider, fd, OPT_ALL);
468 l2tp_write_fields(provider, fd, OPT_L2);
470 l2tp_write_option(fd, "pppoptfile =", pppd_name);
475 static void l2tp_died(struct connman_task *task, int exit_code, void *user_data)
479 vpn_died(task, exit_code, user_data);
481 conf_file = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
485 conf_file = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
490 struct request_input_reply {
491 struct vpn_provider *provider;
492 vpn_provider_password_cb_t callback;
496 static void request_input_reply(DBusMessage *reply, void *user_data)
498 struct request_input_reply *l2tp_reply = user_data;
499 const char *error = NULL;
500 char *username = NULL, *password = NULL;
502 DBusMessageIter iter, dict;
504 DBG("provider %p", l2tp_reply->provider);
506 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
507 error = dbus_message_get_error_name(reply);
511 if (vpn_agent_check_reply_has_dict(reply) == FALSE)
514 dbus_message_iter_init(reply, &iter);
515 dbus_message_iter_recurse(&iter, &dict);
516 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
517 DBusMessageIter entry, value;
520 dbus_message_iter_recurse(&dict, &entry);
521 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
524 dbus_message_iter_get_basic(&entry, &key);
526 if (g_str_equal(key, "Username")) {
527 dbus_message_iter_next(&entry);
528 if (dbus_message_iter_get_arg_type(&entry)
529 != DBUS_TYPE_VARIANT)
531 dbus_message_iter_recurse(&entry, &value);
532 if (dbus_message_iter_get_arg_type(&value)
535 dbus_message_iter_get_basic(&value, &str);
536 username = g_strdup(str);
539 if (g_str_equal(key, "Password")) {
540 dbus_message_iter_next(&entry);
541 if (dbus_message_iter_get_arg_type(&entry)
542 != DBUS_TYPE_VARIANT)
544 dbus_message_iter_recurse(&entry, &value);
545 if (dbus_message_iter_get_arg_type(&value)
548 dbus_message_iter_get_basic(&value, &str);
549 password = g_strdup(str);
552 dbus_message_iter_next(&dict);
556 l2tp_reply->callback(l2tp_reply->provider, username, password, error,
557 l2tp_reply->user_data);
565 typedef void (* request_cb_t)(struct vpn_provider *provider,
566 const char *username, const char *password,
567 const char *error, void *user_data);
569 static int request_input(struct vpn_provider *provider,
570 request_cb_t callback, void *user_data)
572 DBusMessage *message;
573 const char *path, *agent_sender, *agent_path;
574 DBusMessageIter iter;
575 DBusMessageIter dict;
576 struct request_input_reply *l2tp_reply;
579 connman_agent_get_info(&agent_sender, &agent_path);
581 if (provider == NULL || agent_path == NULL || callback == NULL)
584 message = dbus_message_new_method_call(agent_sender, agent_path,
590 dbus_message_iter_init_append(message, &iter);
592 path = vpn_provider_get_path(provider);
593 dbus_message_iter_append_basic(&iter,
594 DBUS_TYPE_OBJECT_PATH, &path);
596 connman_dbus_dict_open(&iter, &dict);
598 vpn_agent_append_user_info(&dict, provider, "L2TP.User");
600 vpn_agent_append_host_and_name(&dict, provider);
602 connman_dbus_dict_close(&iter, &dict);
604 l2tp_reply = g_try_new0(struct request_input_reply, 1);
605 if (l2tp_reply == NULL) {
606 dbus_message_unref(message);
610 l2tp_reply->provider = provider;
611 l2tp_reply->callback = callback;
612 l2tp_reply->user_data = user_data;
614 err = connman_agent_queue_message(provider, message,
615 connman_timeout_input_request(),
616 request_input_reply, l2tp_reply);
617 if (err < 0 && err != -EBUSY) {
618 DBG("error %d sending agent request", err);
619 dbus_message_unref(message);
624 dbus_message_unref(message);
629 static int run_connect(struct vpn_provider *provider,
630 struct connman_task *task, const char *if_name,
631 vpn_provider_connect_cb_t cb, void *user_data,
632 const char *username, const char *password)
634 char *l2tp_name, *pppd_name;
635 int l2tp_fd, pppd_fd;
638 if (username == NULL || password == NULL) {
639 DBG("Cannot connect username %s password %p",
645 vpn_provider_set_string(provider, "L2TP.User", username);
646 vpn_provider_set_string(provider, "L2TP.Password", password);
648 DBG("username %s password %p", username, password);
650 l2tp_name = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
652 l2tp_fd = open(l2tp_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
655 connman_error("Error writing l2tp config");
660 pppd_name = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
662 pppd_fd = open(pppd_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
664 connman_error("Error writing pppd config");
672 l2tp_write_config(provider, pppd_name, l2tp_fd);
674 write_pppd_option(provider, pppd_fd);
676 connman_task_add_argument(task, "-D", NULL);
677 connman_task_add_argument(task, "-c", l2tp_name);
682 err = connman_task_run(task, l2tp_died, provider,
685 connman_error("l2tp failed to start");
692 cb(provider, user_data, err);
697 static void free_private_data(struct l2tp_private_data *data)
699 g_free(data->if_name);
703 static void request_input_cb(struct vpn_provider *provider,
704 const char *username,
705 const char *password,
706 const char *error, void *user_data)
708 struct l2tp_private_data *data = user_data;
710 if (username == NULL || password == NULL)
711 DBG("Requesting username %s or password failed, error %s",
713 else if (error != NULL)
714 DBG("error %s", error);
716 run_connect(provider, data->task, data->if_name, data->cb,
717 data->user_data, username, password);
719 free_private_data(data);
722 static int l2tp_connect(struct vpn_provider *provider,
723 struct connman_task *task, const char *if_name,
724 vpn_provider_connect_cb_t cb, void *user_data)
726 const char *username, *password;
729 if (connman_task_set_notify(task, "getsec",
730 l2tp_get_sec, provider) != 0) {
735 username = vpn_provider_get_string(provider, "L2TP.User");
736 password = vpn_provider_get_string(provider, "L2TP.Password");
738 DBG("user %s password %p", username, password);
740 if (username == NULL || password == NULL) {
741 struct l2tp_private_data *data;
743 data = g_try_new0(struct l2tp_private_data, 1);
748 data->if_name = g_strdup(if_name);
750 data->user_data = user_data;
752 err = request_input(provider, request_input_cb, data);
753 if (err != -EINPROGRESS) {
754 free_private_data(data);
761 return run_connect(provider, task, if_name, cb, user_data,
766 cb(provider, user_data, err);
771 static int l2tp_error_code(int exit_code)
775 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
777 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
781 static void l2tp_disconnect(struct vpn_provider *provider)
783 vpn_provider_set_string(provider, "L2TP.Password", NULL);
786 static struct vpn_driver vpn_driver = {
787 .flags = VPN_FLAG_NO_TUN,
788 .notify = l2tp_notify,
789 .connect = l2tp_connect,
790 .error_code = l2tp_error_code,
792 .disconnect = l2tp_disconnect,
795 static int l2tp_init(void)
797 connection = connman_dbus_get_connection();
799 return vpn_register("l2tp", &vpn_driver, L2TP);
802 static void l2tp_exit(void)
804 vpn_unregister("l2tp");
806 dbus_connection_unref(connection);
809 CONNMAN_PLUGIN_DEFINE(l2tp, "l2tp plugin", VERSION,
810 CONNMAN_PLUGIN_PRIORITY_DEFAULT, l2tp_init, l2tp_exit)