5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
6 * Copyright (C) 2011 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>
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>
49 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
67 const char *vpn_default;
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 },
111 static DBusConnection *connection;
113 static DBusMessage *l2tp_get_sec(struct connman_task *task,
114 DBusMessage *msg, void *user_data)
116 const char *user, *passwd;
117 struct connman_provider *provider = user_data;
119 if (dbus_message_get_no_reply(msg) == FALSE) {
122 user = connman_provider_get_string(provider, "L2TP.User");
123 passwd = connman_provider_get_string(provider, "L2TP.Password");
125 if (user == NULL || strlen(user) == 0 ||
126 passwd == NULL || strlen(passwd) == 0)
129 reply = dbus_message_new_method_return(msg);
133 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
134 DBUS_TYPE_STRING, &passwd,
143 static int l2tp_notify(DBusMessage *msg, struct connman_provider *provider)
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;
151 dbus_message_iter_init(msg, &iter);
153 dbus_message_iter_get_basic(&iter, &reason);
154 dbus_message_iter_next(&iter);
157 connman_error("No provider found");
158 return VPN_STATE_FAILURE;
161 if (strcmp(reason, "auth failed") == 0)
162 return VPN_STATE_AUTH_FAILURE;
164 if (strcmp(reason, "connect"))
165 return VPN_STATE_DISCONNECT;
167 dbus_message_iter_recurse(&iter, &dict);
169 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
170 DBusMessageIter entry;
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);
177 DBG("%s = %s", key, value);
179 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
180 connman_provider_set_string(provider, "Address", value);
181 addressv4 = g_strdup(value);
184 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
185 connman_provider_set_string(provider, "Netmask", value);
186 netmask = g_strdup(value);
189 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
190 connman_provider_set_string(provider, "DNS", value);
191 nameservers = g_strdup(value);
194 if (!strcmp(key, "INTERNAL_IFNAME"))
195 ifname = g_strdup(value);
197 dbus_message_iter_next(&dict);
200 if (vpn_set_ifname(provider, ifname) < 0) {
205 return VPN_STATE_FAILURE;
208 if (addressv4 != NULL)
209 ipaddress = connman_ipaddress_alloc(AF_INET);
213 if (ipaddress == NULL) {
214 connman_error("No IP address for provider");
218 return VPN_STATE_FAILURE;
221 value = connman_provider_get_string(provider, "Host");
223 connman_provider_set_string(provider, "Gateway", value);
224 gateway = g_strdup(value);
227 if (addressv4 != NULL)
228 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
231 connman_provider_set_ipaddress(provider, ipaddress);
232 connman_provider_set_nameservers(provider, nameservers);
238 connman_ipaddress_free(ipaddress);
240 return VPN_STATE_CONNECT;
243 static ssize_t full_write(int fd, const void *buf, size_t len)
248 byte_write = write(fd, buf, len);
249 if (byte_write < 0) {
250 connman_error("failed to write config to l2tp: %s\n",
261 static ssize_t l2tp_write_bool_option(int fd,
262 const char *key, const char *value)
267 if (key != NULL && value != NULL) {
268 if (strcmp(value, "yes") == 0) {
269 buf = g_strdup_printf("%s\n", key);
270 ret = full_write(fd, buf, strlen(buf));
279 static int l2tp_write_option(int fd, const char *key, const char *value)
286 buf = g_strdup_printf("%s %s\n", key, value);
288 buf = g_strdup_printf("%s\n", key);
290 ret = full_write(fd, buf, strlen(buf));
298 static int l2tp_write_section(int fd, const char *key, const char *value)
303 if (key != NULL && value != NULL) {
304 buf = g_strdup_printf("%s = %s\n", key, value);
305 ret = full_write(fd, buf, strlen(buf));
313 static int write_pppd_option(struct connman_provider *provider, int fd)
318 l2tp_write_option(fd, "nodetach", NULL);
319 l2tp_write_option(fd, "lock", NULL);
320 l2tp_write_option(fd, "usepeerdns", NULL);
321 l2tp_write_option(fd, "noipdefault", NULL);
322 l2tp_write_option(fd, "noauth", NULL);
323 l2tp_write_option(fd, "nodefaultroute", NULL);
324 l2tp_write_option(fd, "ipparam", "l2tp_plugin");
326 for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
327 if (pppd_options[i].sub != OPT_ALL &&
328 pppd_options[i].sub != OPT_PPPD)
331 opt_s = connman_provider_get_string(provider,
332 pppd_options[i].cm_opt);
334 opt_s = pppd_options[i].vpn_default;
339 if (pppd_options[i].type == OPT_STRING)
340 l2tp_write_option(fd,
341 pppd_options[i].pppd_opt, opt_s);
342 else if (pppd_options[i].type == OPT_BOOL)
343 l2tp_write_bool_option(fd,
344 pppd_options[i].pppd_opt, opt_s);
347 l2tp_write_option(fd, "plugin",
348 SCRIPTDIR "/libppp-plugin.so");
354 static int l2tp_write_fields(struct connman_provider *provider,
360 for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
361 if (pppd_options[i].sub != sub)
364 opt_s = connman_provider_get_string(provider,
365 pppd_options[i].cm_opt);
367 opt_s = pppd_options[i].vpn_default;
372 if (pppd_options[i].type == OPT_STRING)
373 l2tp_write_section(fd,
374 pppd_options[i].pppd_opt, opt_s);
375 else if (pppd_options[i].type == OPT_BOOL)
376 l2tp_write_bool_option(fd,
377 pppd_options[i].pppd_opt, opt_s);
383 static int l2tp_write_config(struct connman_provider *provider,
384 const char *pppd_name, int fd)
388 l2tp_write_option(fd, "[global]", NULL);
389 l2tp_write_fields(provider, fd, OPT_L2G);
391 l2tp_write_option(fd, "[lac l2tp]", NULL);
393 option = connman_provider_get_string(provider, "Host");
394 l2tp_write_option(fd, "lns =", option);
396 l2tp_write_fields(provider, fd, OPT_ALL);
397 l2tp_write_fields(provider, fd, OPT_L2);
399 l2tp_write_option(fd, "pppoptfile =", pppd_name);
404 static void l2tp_died(struct connman_task *task, int exit_code, void *user_data)
408 vpn_died(task, exit_code, user_data);
410 conf_file = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
414 conf_file = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
419 static int l2tp_connect(struct connman_provider *provider,
420 struct connman_task *task, const char *if_name)
423 char *l2tp_name, *pppd_name;
424 int l2tp_fd, pppd_fd;
427 if (connman_task_set_notify(task, "getsec",
428 l2tp_get_sec, provider))
431 host = connman_provider_get_string(provider, "Host");
433 connman_error("Host not set; cannot enable VPN");
437 l2tp_name = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
439 l2tp_fd = open(l2tp_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
442 connman_error("Error writing l2tp config");
446 pppd_name = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
448 pppd_fd = open(pppd_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
450 connman_error("Error writing pppd config");
457 l2tp_write_config(provider, pppd_name, l2tp_fd);
459 write_pppd_option(provider, pppd_fd);
461 connman_task_add_argument(task, "-D", NULL);
462 connman_task_add_argument(task, "-c", l2tp_name);
467 err = connman_task_run(task, l2tp_died, provider,
470 connman_error("l2tp failed to start");
477 static int l2tp_error_code(int exit_code)
481 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
483 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
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,
494 static int l2tp_init(void)
496 connection = connman_dbus_get_connection();
498 return vpn_register("l2tp", &vpn_driver, L2TP);
501 static void l2tp_exit(void)
503 vpn_unregister("l2tp");
505 dbus_connection_unref(connection);
508 CONNMAN_PLUGIN_DEFINE(l2tp, "l2tp plugin", VERSION,
509 CONNMAN_PLUGIN_PRIORITY_DEFAULT, l2tp_init, l2tp_exit)