1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2008 Novell, Inc.
* Copyright (C) 2008 Red Hat, Inc.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <net/if.h>
#include <sys/stat.h>
#include <linux/ppp_defs.h>
#ifndef aligned_u64
#define aligned_u64 unsigned long long __attribute__((aligned(8)))
#endif
#include <linux/if_ppp.h>
#include "NetworkManager.h"
#include "nm-glib-compat.h"
#include "nm-ppp-manager.h"
#include "nm-setting-connection.h"
#include "nm-setting-ppp.h"
#include "nm-setting-pppoe.h"
#include "nm-setting-gsm.h"
#include "nm-setting-cdma.h"
#include "nm-dbus-manager.h"
#include "nm-logging.h"
#include "nm-marshal.h"
static void impl_ppp_manager_need_secrets (NMPPPManager *manager,
DBusGMethodInvocation *context);
static gboolean impl_ppp_manager_set_state (NMPPPManager *manager,
guint32 state,
GError **err);
static gboolean impl_ppp_manager_set_ip4_config (NMPPPManager *manager,
GHashTable *config,
GError **err);
#include "nm-ppp-manager-glue.h"
#define NM_PPPD_PLUGIN PLUGINDIR "/nm-pppd-plugin.so"
#define PPP_MANAGER_SECRET_TRIES "ppp-manager-secret-tries"
typedef struct {
GPid pid;
NMDBusManager *dbus_manager;
char *dbus_path;
char *parent_iface;
NMActRequest *act_req;
DBusGMethodInvocation *pending_secrets_context;
guint32 ppp_watch_id;
guint32 ppp_timeout_handler;
/* Monitoring */
char *ip_iface;
int monitor_fd;
guint monitor_id;
} NMPPPManagerPrivate;
#define NM_PPP_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_PPP_MANAGER, NMPPPManagerPrivate))
G_DEFINE_TYPE (NMPPPManager, nm_ppp_manager, G_TYPE_OBJECT)
enum {
STATE_CHANGED,
IP4_CONFIG,
STATS,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_PARENT_IFACE,
LAST_PROP
};
typedef enum {
NM_PPP_MANAGER_ERROR_UNKOWN
} NMPPPManagerError;
GQuark
nm_ppp_manager_error_quark (void)
{
static GQuark quark;
if (!quark)
quark = g_quark_from_static_string ("nm_ppp_manager_error");
return quark;
}
static void
nm_ppp_manager_init (NMPPPManager *manager)
{
}
static GObject *
constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
{
GObject *object;
NMPPPManagerPrivate *priv;
DBusGConnection *connection;
static guint32 counter = 0;
object = G_OBJECT_CLASS (nm_ppp_manager_parent_class)->constructor (type,
n_construct_params,
construct_params);
if (!object)
return NULL;
priv = NM_PPP_MANAGER_GET_PRIVATE (object);
priv->dbus_manager = nm_dbus_manager_get ();
if (!priv->dbus_manager) {
g_object_unref (object);
return NULL;
}
connection = nm_dbus_manager_get_connection (priv->dbus_manager);
priv->dbus_path = g_strdup_printf (NM_DBUS_PATH "/PPP/%d", counter++);
dbus_g_connection_register_g_object (connection, priv->dbus_path, object);
return object;
}
static void
dispose (GObject *object)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (object);
nm_ppp_manager_stop (NM_PPP_MANAGER (object));
g_object_unref (priv->act_req);
g_object_unref (priv->dbus_manager);
G_OBJECT_CLASS (nm_ppp_manager_parent_class)->dispose (object);
}
static void
finalize (GObject *object)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (object);
g_free (priv->ip_iface);
g_free (priv->parent_iface);
G_OBJECT_CLASS (nm_ppp_manager_parent_class)->finalize (object);
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (object);
switch (prop_id) {
case PROP_PARENT_IFACE:
if (priv->parent_iface)
g_free (priv->parent_iface);
priv->parent_iface = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (object);
switch (prop_id) {
case PROP_PARENT_IFACE:
g_value_set_string (value, priv->parent_iface);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
nm_ppp_manager_class_init (NMPPPManagerClass *manager_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
g_type_class_add_private (manager_class, sizeof (NMPPPManagerPrivate));
object_class->constructor = constructor;
object_class->dispose = dispose;
object_class->finalize = finalize;
object_class->get_property = get_property;
object_class->set_property = set_property;
/* Properties */
g_object_class_install_property
(object_class, PROP_PARENT_IFACE,
g_param_spec_string (NM_PPP_MANAGER_PARENT_IFACE,
"ParentIface",
"Parent interface",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/* signals */
signals[STATE_CHANGED] =
g_signal_new ("state-changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMPPPManagerClass, state_changed),
NULL, NULL,
g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1,
G_TYPE_UINT);
signals[IP4_CONFIG] =
g_signal_new ("ip4-config",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMPPPManagerClass, ip4_config),
NULL, NULL,
_nm_marshal_VOID__STRING_OBJECT,
G_TYPE_NONE, 2,
G_TYPE_STRING,
G_TYPE_OBJECT);
signals[STATS] =
g_signal_new ("stats",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMPPPManagerClass, stats),
NULL, NULL,
_nm_marshal_VOID__UINT_UINT,
G_TYPE_NONE, 2,
G_TYPE_UINT, G_TYPE_UINT);
dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (manager_class),
&dbus_glib_nm_ppp_manager_object_info);
}
NMPPPManager *
nm_ppp_manager_new (const char *iface)
{
g_return_val_if_fail (iface != NULL, NULL);
return (NMPPPManager *) g_object_new (NM_TYPE_PPP_MANAGER,
NM_PPP_MANAGER_PARENT_IFACE, iface,
NULL);
}
/*******************************************/
static gboolean
monitor_cb (gpointer user_data)
{
NMPPPManager *manager = NM_PPP_MANAGER (user_data);
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
struct ifpppstatsreq req;
memset (&req, 0, sizeof (req));
req.stats_ptr = (caddr_t) &req.stats;
strncpy (req.ifr__name, priv->ip_iface, sizeof (req.ifr__name));
if (ioctl (priv->monitor_fd, SIOCGPPPSTATS, &req) < 0) {
nm_log_warn (LOGD_PPP, "could not read ppp stats: %s", strerror (errno));
} else {
g_signal_emit (manager, signals[STATS], 0,
req.stats.p.ppp_ibytes,
req.stats.p.ppp_obytes);
}
return TRUE;
}
static void
monitor_stats (NMPPPManager *manager)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
priv->monitor_fd = socket (AF_INET, SOCK_DGRAM, 0);
if (priv->monitor_fd > 0)
priv->monitor_id = g_timeout_add_seconds (5, monitor_cb, manager);
else
nm_log_warn (LOGD_PPP, "could not monitor PPP stats: %s", strerror (errno));
}
/*******************************************/
static void
remove_timeout_handler (NMPPPManager *manager)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
if (priv->ppp_timeout_handler) {
g_source_remove (priv->ppp_timeout_handler);
priv->ppp_timeout_handler = 0;
}
}
static void
impl_ppp_manager_need_secrets (NMPPPManager *manager,
DBusGMethodInvocation *context)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
NMConnection *connection;
NMSettingConnection *s_con;
const char *connection_type;
const char *setting_name;
guint32 tries;
GPtrArray *hints = NULL;
const char *hint1 = NULL, *hint2 = NULL;
connection = nm_act_request_get_connection (priv->act_req);
s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
g_assert (s_con);
connection_type = nm_setting_connection_get_connection_type (s_con);
g_assert (connection_type);
nm_connection_clear_secrets (connection);
setting_name = nm_connection_need_secrets (connection, &hints);
if (!setting_name) {
NMSetting *setting;
setting = nm_connection_get_setting_by_name (connection, connection_type);
if (setting) {
const char *username = NULL;
const char *password = NULL;
/* FIXME: push this down to the settings and keep PPP manager generic */
if (NM_IS_SETTING_PPPOE (setting)) {
username = nm_setting_pppoe_get_username (NM_SETTING_PPPOE (setting));
password = nm_setting_pppoe_get_password (NM_SETTING_PPPOE (setting));
} else if (NM_IS_SETTING_GSM (setting)) {
username = nm_setting_gsm_get_username (NM_SETTING_GSM (setting));
password = nm_setting_gsm_get_password (NM_SETTING_GSM (setting));
} else if (NM_IS_SETTING_CDMA (setting)) {
username = nm_setting_cdma_get_username (NM_SETTING_CDMA (setting));
password = nm_setting_cdma_get_password (NM_SETTING_CDMA (setting));
}
/* If secrets are not required, send the existing username and password
* back to the PPP plugin immediately.
*/
priv->pending_secrets_context = context;
nm_ppp_manager_update_secrets (manager,
priv->parent_iface,
username ? username : "",
password ? password : "",
NULL);
} else {
GError *err = NULL;
g_set_error (&err, NM_PPP_MANAGER_ERROR, NM_PPP_MANAGER_ERROR_UNKOWN,
"Missing type-specific setting; no secrets could be found.");
nm_log_warn (LOGD_PPP, "%s", err->message);
dbus_g_method_return_error (context, err);
}
return;
}
/* Extract hints */
if (hints) {
if (hints->len > 0)
hint1 = g_ptr_array_index (hints, 0);
if (hints->len > 1)
hint2 = g_ptr_array_index (hints, 1);
}
tries = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (connection), PPP_MANAGER_SECRET_TRIES));
/* Only ask for completely new secrets after retrying them once; some PPP
* servers (T-Mobile USA) appear to ask a few times when they actually don't
* even care what you pass back.
*/
nm_act_request_get_secrets (priv->act_req,
setting_name,
tries > 1 ? TRUE : FALSE,
SECRETS_CALLER_PPP,
hint1,
hint2);
g_object_set_data (G_OBJECT (connection), PPP_MANAGER_SECRET_TRIES, GUINT_TO_POINTER (++tries));
priv->pending_secrets_context = context;
if (hints)
g_ptr_array_free (hints, TRUE);
}
static gboolean impl_ppp_manager_set_state (NMPPPManager *manager,
guint32 state,
GError **err)
{
g_signal_emit (manager, signals[STATE_CHANGED], 0, state);
return TRUE;
}
static gboolean
impl_ppp_manager_set_ip4_config (NMPPPManager *manager,
GHashTable *config_hash,
GError **err)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
NMConnection *connection;
NMSettingPPP *s_ppp;
NMIP4Config *config;
NMIP4Address *addr;
GValue *val;
int i;
nm_log_info (LOGD_PPP, "PPP manager(IP Config Get) reply received.");
remove_timeout_handler (manager);
config = nm_ip4_config_new ();
addr = nm_ip4_address_new ();
nm_ip4_address_set_prefix (addr, 32);
val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_GATEWAY);
if (val) {
nm_ip4_address_set_gateway (addr, g_value_get_uint (val));
nm_ip4_config_set_ptp_address (config, g_value_get_uint (val));
}
val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_ADDRESS);
if (val)
nm_ip4_address_set_address (addr, g_value_get_uint (val));
val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_PREFIX);
if (val)
nm_ip4_address_set_prefix (addr, g_value_get_uint (val));
if (nm_ip4_address_get_address (addr) && nm_ip4_address_get_prefix (addr)) {
nm_ip4_config_take_address (config, addr);
} else {
nm_log_err (LOGD_PPP, "invalid IPv4 address received!");
nm_ip4_address_unref (addr);
goto out;
}
val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_DNS);
if (val) {
GArray *dns = (GArray *) g_value_get_boxed (val);
for (i = 0; i < dns->len; i++)
nm_ip4_config_add_nameserver (config, g_array_index (dns, guint, i));
}
val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_WINS);
if (val) {
GArray *wins = (GArray *) g_value_get_boxed (val);
for (i = 0; i < wins->len; i++)
nm_ip4_config_add_wins (config, g_array_index (wins, guint, i));
}
val = (GValue *) g_hash_table_lookup (config_hash, NM_PPP_IP4_CONFIG_INTERFACE);
if (!val || !G_VALUE_HOLDS_STRING (val)) {
nm_log_err (LOGD_PPP, "no interface received!");
goto out;
}
priv->ip_iface = g_value_dup_string (val);
/* Got successful IP4 config; obviously the secrets worked */
connection = nm_act_request_get_connection (priv->act_req);
g_assert (connection);
g_object_set_data (G_OBJECT (connection), PPP_MANAGER_SECRET_TRIES, NULL);
/* Merge in custom MTU */
s_ppp = (NMSettingPPP *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP);
if (s_ppp) {
guint32 mtu = nm_setting_ppp_get_mtu (s_ppp);
if (mtu)
nm_ip4_config_set_mtu (config, mtu);
}
/* Push the IP4 config up to the device */
g_signal_emit (manager, signals[IP4_CONFIG], 0, priv->ip_iface, config);
monitor_stats (manager);
out:
g_object_unref (config);
return TRUE;
}
/*******************************************/
typedef struct {
GPtrArray *array;
GStringChunk *chunk;
} NMCmdLine;
static NMCmdLine *
nm_cmd_line_new (void)
{
NMCmdLine *cmd;
cmd = g_slice_new (NMCmdLine);
cmd->array = g_ptr_array_new ();
cmd->chunk = g_string_chunk_new (1024);
return cmd;
}
static void
nm_cmd_line_destroy (NMCmdLine *cmd)
{
g_ptr_array_free (cmd->array, TRUE);
g_string_chunk_free (cmd->chunk);
g_slice_free (NMCmdLine, cmd);
}
static char *
nm_cmd_line_to_str (NMCmdLine *cmd)
{
char *str;
g_ptr_array_add (cmd->array, NULL);
str = g_strjoinv (" ", (gchar **) cmd->array->pdata);
g_ptr_array_remove_index (cmd->array, cmd->array->len - 1);
return str;
}
static void
nm_cmd_line_add_string (NMCmdLine *cmd, const char *str)
{
g_ptr_array_add (cmd->array, g_string_chunk_insert (cmd->chunk, str));
}
static void
nm_cmd_line_add_int (NMCmdLine *cmd, int i)
{
char *str;
str = g_strdup_printf ("%d", i);
nm_cmd_line_add_string (cmd, str);
g_free (str);
}
/*******************************************/
static inline const char *
nm_find_pppd (void)
{
static const char *pppd_binary_paths[] = {
"/usr/local/sbin/pppd",
"/usr/sbin/pppd",
"/sbin/pppd",
NULL
};
const char **pppd_binary = pppd_binary_paths;
while (*pppd_binary != NULL) {
if (g_file_test (*pppd_binary, G_FILE_TEST_EXISTS))
break;
pppd_binary++;
}
return *pppd_binary;
}
static void
ppp_exit_code (guint pppd_exit_status, GPid pid)
{
const char *msg;
switch (pppd_exit_status) {
case 1:
msg = "Fatal pppd error";
break;
case 2:
msg = "pppd options error";
break;
case 3:
msg = "No root priv error";
break;
case 4:
msg = "No ppp module error";
break;
case 5:
msg = "pppd received a signal";
break;
case 6:
msg = "Serial port lock failed";
break;
case 7:
msg = "Serial port open failed";
break;
case 8:
msg = "Connect script failed";
break;
case 9:
msg = "Pty program error";
break;
case 10:
msg = "PPP negotiation failed";
break;
case 11:
msg = "Peer didn't authenticatie itself";
break;
case 12:
msg = "Link idle: Idle Seconds reached.";
break;
case 13:
msg = "Connect time limit reached.";
break;
case 14:
msg = "Callback negotiated, call should come back.";
break;
case 15:
msg = "Lack of LCP echo responses";
break;
case 16:
msg = "A modem hung up the phone";
break;
case 17:
msg = "Loopback detected";
break;
case 18:
msg = "The init script failed";
break;
case 19:
msg = "Authentication error.\n"
"We failed to authenticate ourselves to the peer.\n"
"Maybe bad account or password?";
break;
default:
msg = "Unknown error";
}
nm_log_warn (LOGD_PPP, "pppd pid %d exited with error: %s", pid, msg);
}
static void
ppp_watch_cb (GPid pid, gint status, gpointer user_data)
{
NMPPPManager *manager = NM_PPP_MANAGER (user_data);
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
guint err;
g_assert (pid == priv->pid);
if (WIFEXITED (status)) {
err = WEXITSTATUS (status);
if (err != 0)
ppp_exit_code (err, priv->pid);
} else if (WIFSTOPPED (status)) {
nm_log_info (LOGD_PPP, "pppd pid %d stopped unexpectedly with signal %d", priv->pid, WSTOPSIG (status));
} else if (WIFSIGNALED (status)) {
nm_log_info (LOGD_PPP, "pppd pid %d died with signal %d", priv->pid, WTERMSIG (status));
} else
nm_log_info (LOGD_PPP, "pppd pid %d died from an unknown cause", priv->pid);
nm_log_dbg (LOGD_PPP, "pppd pid %d cleaned up", priv->pid);
priv->pid = 0;
g_signal_emit (manager, signals[STATE_CHANGED], 0, NM_PPP_STATUS_DEAD);
}
static gboolean
pppd_timed_out (gpointer data)
{
NMPPPManager *manager = NM_PPP_MANAGER (data);
nm_log_warn (LOGD_PPP, "pppd timed out or didn't initialize our dbus module");
nm_ppp_manager_stop (manager);
g_signal_emit (manager, signals[STATE_CHANGED], 0, NM_PPP_STATUS_DEAD);
return FALSE;
}
static NMCmdLine *
create_pppd_cmd_line (NMPPPManager *self,
NMSettingPPP *setting,
NMSettingPPPOE *pppoe,
const char *ppp_name,
GError **err)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (self);
const char *ppp_binary;
NMCmdLine *cmd;
const char *ppp_debug;
ppp_binary = nm_find_pppd ();
if (!ppp_binary) {
g_set_error (err, NM_PPP_MANAGER_ERROR, NM_PPP_MANAGER_ERROR,
"Could not find ppp binary.");
return NULL;
}
/* Create pppd command line */
cmd = nm_cmd_line_new ();
nm_cmd_line_add_string (cmd, ppp_binary);
nm_cmd_line_add_string (cmd, "nodetach");
nm_cmd_line_add_string (cmd, "lock");
/* NM handles setting the default route */
nm_cmd_line_add_string (cmd, "nodefaultroute");
ppp_debug = getenv ("NM_PPP_DEBUG");
if (ppp_debug)
nm_cmd_line_add_string (cmd, "debug");
if (ppp_name) {
nm_cmd_line_add_string (cmd, "user");
nm_cmd_line_add_string (cmd, ppp_name);
}
if (pppoe) {
char *dev_str;
const char *pppoe_service;
nm_cmd_line_add_string (cmd, "plugin");
nm_cmd_line_add_string (cmd, "rp-pppoe.so");
dev_str = g_strdup_printf ("nic-%s", priv->parent_iface);
nm_cmd_line_add_string (cmd, dev_str);
g_free (dev_str);
pppoe_service = nm_setting_pppoe_get_service (pppoe);
if (pppoe_service) {
nm_cmd_line_add_string (cmd, "rp_pppoe_service");
nm_cmd_line_add_string (cmd, pppoe_service);
}
} else {
nm_cmd_line_add_string (cmd, priv->parent_iface);
/* Don't send some random address as the local address */
nm_cmd_line_add_string (cmd, "noipdefault");
}
if (nm_setting_ppp_get_baud (setting))
nm_cmd_line_add_int (cmd, nm_setting_ppp_get_baud (setting));
/* noauth by default, because we certainly don't have any information
* with which to verify anything the peer gives us if we ask it to
* authenticate itself, which is what 'auth' really means.
*/
nm_cmd_line_add_string (cmd, "noauth");
if (nm_setting_ppp_get_refuse_eap (setting))
nm_cmd_line_add_string (cmd, "refuse-eap");
if (nm_setting_ppp_get_refuse_pap (setting))
nm_cmd_line_add_string (cmd, "refuse-pap");
if (nm_setting_ppp_get_refuse_chap (setting))
nm_cmd_line_add_string (cmd, "refuse-chap");
if (nm_setting_ppp_get_refuse_mschap (setting))
nm_cmd_line_add_string (cmd, "refuse-mschap");
if (nm_setting_ppp_get_refuse_mschapv2 (setting))
nm_cmd_line_add_string (cmd, "refuse-mschap-v2");
if (nm_setting_ppp_get_nobsdcomp (setting))
nm_cmd_line_add_string (cmd, "nobsdcomp");
if (nm_setting_ppp_get_nodeflate (setting))
nm_cmd_line_add_string (cmd, "nodeflate");
if (nm_setting_ppp_get_require_mppe (setting))
nm_cmd_line_add_string (cmd, "require-mppe");
if (nm_setting_ppp_get_require_mppe_128 (setting))
nm_cmd_line_add_string (cmd, "require-mppe-128");
if (nm_setting_ppp_get_mppe_stateful (setting))
nm_cmd_line_add_string (cmd, "mppe-stateful");
if (nm_setting_ppp_get_crtscts (setting))
nm_cmd_line_add_string (cmd, "crtscts");
/* Always ask for DNS, we don't have to use them if the connection
* overrides the returned servers.
*/
nm_cmd_line_add_string (cmd, "usepeerdns");
if (nm_setting_ppp_get_mru (setting)) {
nm_cmd_line_add_string (cmd, "mru");
nm_cmd_line_add_int (cmd, nm_setting_ppp_get_mru (setting));
}
if (nm_setting_ppp_get_mtu (setting)) {
nm_cmd_line_add_string (cmd, "mtu");
nm_cmd_line_add_int (cmd, nm_setting_ppp_get_mtu (setting));
}
nm_cmd_line_add_string (cmd, "lcp-echo-failure");
nm_cmd_line_add_int (cmd, nm_setting_ppp_get_lcp_echo_failure (setting));
nm_cmd_line_add_string (cmd, "lcp-echo-interval");
nm_cmd_line_add_int (cmd, nm_setting_ppp_get_lcp_echo_interval (setting));
nm_cmd_line_add_string (cmd, "ipparam");
nm_cmd_line_add_string (cmd, priv->dbus_path);
nm_cmd_line_add_string (cmd, "plugin");
nm_cmd_line_add_string (cmd, NM_PPPD_PLUGIN);
return cmd;
}
static void
pppd_child_setup (gpointer user_data G_GNUC_UNUSED)
{
/* We are in the child process at this point */
pid_t pid = getpid ();
setpgid (pid, pid);
}
static void
pppoe_fill_defaults (NMSettingPPP *setting)
{
if (!nm_setting_ppp_get_mtu (setting))
g_object_set (setting, NM_SETTING_PPP_MTU, (guint32) 1492, NULL);
if (!nm_setting_ppp_get_mru (setting))
g_object_set (setting, NM_SETTING_PPP_MRU, (guint32) 1492, NULL);
if (!nm_setting_ppp_get_lcp_echo_interval (setting))
g_object_set (setting, NM_SETTING_PPP_LCP_ECHO_INTERVAL, (guint32) 20, NULL);
if (!nm_setting_ppp_get_lcp_echo_failure (setting))
g_object_set (setting, NM_SETTING_PPP_LCP_ECHO_FAILURE, (guint32) 3, NULL);
g_object_set (setting,
NM_SETTING_PPP_NOAUTH, TRUE,
NM_SETTING_PPP_NODEFLATE, TRUE,
NULL);
/* FIXME: These commented settings should be set as well, update NMSettingPPP first. */
#if 0
setting->noipdefault = TRUE;
setting->default_asyncmap = TRUE;
setting->defaultroute = TRUE;
setting->hide_password = TRUE;
setting->noaccomp = TRUE;
setting->nopcomp = TRUE;
setting->novj = TRUE;
setting->novjccomp = TRUE;
#endif
}
gboolean
nm_ppp_manager_start (NMPPPManager *manager,
NMActRequest *req,
const char *ppp_name,
guint32 timeout_secs,
GError **err)
{
NMPPPManagerPrivate *priv;
NMConnection *connection;
NMSettingPPP *ppp_setting;
NMSettingPPPOE *pppoe_setting;
NMCmdLine *ppp_cmd;
char *cmd_str;
struct stat st;
int ignored;
g_return_val_if_fail (NM_IS_PPP_MANAGER (manager), FALSE);
g_return_val_if_fail (NM_IS_ACT_REQUEST (req), FALSE);
/* Make sure /dev/ppp exists (bgo #533064) */
if (stat ("/dev/ppp", &st) || !S_ISCHR (st.st_mode))
ignored = system ("/sbin/modprobe ppp_generic");
connection = nm_act_request_get_connection (req);
ppp_setting = NM_SETTING_PPP (nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP));
g_return_val_if_fail (ppp_setting != NULL, FALSE);
pppoe_setting = (NMSettingPPPOE *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPPOE);
if (pppoe_setting)
pppoe_fill_defaults (ppp_setting);
ppp_cmd = create_pppd_cmd_line (manager, ppp_setting, pppoe_setting, ppp_name, err);
if (!ppp_cmd)
return FALSE;
g_ptr_array_add (ppp_cmd->array, NULL);
priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
nm_log_info (LOGD_PPP, "starting PPP connection");
cmd_str = nm_cmd_line_to_str (ppp_cmd);
nm_log_dbg (LOGD_PPP, "command line: %s", cmd_str);
g_free (cmd_str);
priv->pid = 0;
if (!g_spawn_async (NULL, (char **) ppp_cmd->array->pdata, NULL,
G_SPAWN_DO_NOT_REAP_CHILD,
pppd_child_setup,
NULL, &priv->pid, err)) {
goto out;
}
nm_log_info (LOGD_PPP, "pppd started with pid %d", priv->pid);
priv->ppp_watch_id = g_child_watch_add (priv->pid, (GChildWatchFunc) ppp_watch_cb, manager);
priv->ppp_timeout_handler = g_timeout_add_seconds (timeout_secs, pppd_timed_out, manager);
priv->act_req = g_object_ref (req);
out:
if (ppp_cmd)
nm_cmd_line_destroy (ppp_cmd);
return priv->pid > 0;
}
void
nm_ppp_manager_update_secrets (NMPPPManager *manager,
const char *device,
const char *username,
const char *password,
const char *error_message)
{
NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
g_return_if_fail (NM_IS_PPP_MANAGER (manager));
g_return_if_fail (device != NULL);
g_return_if_fail (priv->pending_secrets_context != NULL);
if (error_message) {
g_return_if_fail (username == NULL);
g_return_if_fail (password == NULL);
} else {
g_return_if_fail (username != NULL);
g_return_if_fail (password != NULL);
}
if (error_message) {
GError *err = NULL;
g_set_error (&err, NM_PPP_MANAGER_ERROR, NM_PPP_MANAGER_ERROR_UNKOWN, "%s", error_message);
nm_log_warn (LOGD_PPP, "%s", error_message);
dbus_g_method_return_error (priv->pending_secrets_context, err);
g_error_free (err);
} else {
/* This is sort of a hack but...
pppd plugin only ever needs username and password.
Passing the full connection there would mean some bloat:
the plugin would need to link against libnm-util just to parse this.
So instead, let's just send what it needs */
dbus_g_method_return (priv->pending_secrets_context, username, password);
}
priv->pending_secrets_context = NULL;
}
static gboolean
ensure_killed (gpointer data)
{
int pid = GPOINTER_TO_INT (data);
if (kill (pid, 0) == 0)
kill (pid, SIGKILL);
/* ensure the child is reaped */
nm_log_dbg (LOGD_PPP, "waiting for pppd pid %d to exit", pid);
waitpid (pid, NULL, 0);
nm_log_dbg (LOGD_PPP, "pppd pid %d cleaned up", pid);
return FALSE;
}
void
nm_ppp_manager_stop (NMPPPManager *manager)
{
NMPPPManagerPrivate *priv;
g_return_if_fail (NM_IS_PPP_MANAGER (manager));
priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
if (priv->monitor_id) {
g_source_remove (priv->monitor_id);
priv->monitor_id = 0;
}
if (priv->monitor_fd) {
/* Get the stats one last time */
monitor_cb (manager);
close (priv->monitor_fd);
priv->monitor_fd = 0;
}
if (priv->ppp_timeout_handler) {
g_source_remove (priv->ppp_timeout_handler);
priv->ppp_timeout_handler = 0;
}
if (priv->ppp_watch_id) {
g_source_remove (priv->ppp_watch_id);
priv->ppp_watch_id = 0;
}
if (priv->pid) {
if (kill (priv->pid, SIGTERM) == 0)
g_timeout_add_seconds (2, ensure_killed, GINT_TO_POINTER (priv->pid));
else {
kill (priv->pid, SIGKILL);
/* ensure the child is reaped */
nm_log_dbg (LOGD_PPP, "waiting for pppd pid %d to exit", priv->pid);
waitpid (priv->pid, NULL, 0);
nm_log_dbg (LOGD_PPP, "pppd pid %d cleaned up", priv->pid);
}
priv->pid = 0;
}
}
|