-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.c
More file actions
3037 lines (2545 loc) · 77.5 KB
/
Copy pathcli.c
File metadata and controls
3037 lines (2545 loc) · 77.5 KB
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
// L2TPNS Command Line Interface
// vim: sw=8 ts=8
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <syslog.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <dlfcn.h>
#include <netdb.h>
#include <libcli.h>
#include "l2tpns.h"
#include "constants.h"
#include "util.h"
#include "cluster.h"
#include "tbf.h"
#include "ll.h"
#ifdef BGP
#include "bgp.h"
#endif
extern tunnelt *tunnel;
extern bundlet *bundle;
extern sessiont *session;
extern radiust *radius;
extern ippoolt *ip_address_pool;
extern struct Tstats *_statistics;
static struct cli_def *cli = NULL;
extern configt *config;
extern config_descriptt config_values[];
#ifdef RINGBUFFER
extern struct Tringbuffer *ringbuffer;
#endif
extern struct cli_session_actions *cli_session_actions;
extern struct cli_tunnel_actions *cli_tunnel_actions;
extern tbft *filter_list;
extern ip_filtert *ip_filters;
struct
{
char critical;
char error;
char warning;
char info;
char calls;
char data;
} debug_flags;
#ifdef RINGBUFFER
static int debug_rb_tail;
static char *debug_levels[] = {
"CRIT",
"ERROR",
"WARN",
"INFO",
"CALL",
"DATA",
};
#endif
static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_shutdown(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_reload(struct cli_def *cli, char *command, char **argv, int argc);
static int regular_stuff(struct cli_def *cli);
#ifdef STATISTICS
static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
#endif /* STATISTICS */
#ifdef BGP
#define MODE_CONFIG_BGP 8
static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc);
#endif /* BGP */
#define MODE_CONFIG_NACL 9
static int cmd_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_no_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_ip_access_list_rule(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_filter(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_no_filter(struct cli_def *cli, char *command, char **argv, int argc);
static int cmd_show_access_list(struct cli_def *cli, char *command, char **argv, int argc);
/* match if b is a substr of a */
#define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
void init_cli(char *hostname)
{
FILE *f;
char buf[4096];
struct cli_command *c;
struct cli_command *c2;
int on = 1;
struct sockaddr_in addr;
cli = cli_init();
if (hostname && *hostname)
cli_set_hostname(cli, hostname);
else
cli_set_hostname(cli, "l2tpns");
c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
#ifdef BGP
cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
#endif /* BGP */
cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Show the currently running configuration");
cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
cli_register_command(cli, c, "users", cmd_show_users, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of all connected users or details of selected user");
cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
cli_register_command(cli, c, "access-list", cmd_show_access_list, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show named access-list");
c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
#ifdef STATISTICS
cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
#endif
cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
cli_register_command(cli, NULL, "shutdown", cmd_shutdown, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Shutdown l2tpns daemon and exit");
cli_register_command(cli, NULL, "reload", cmd_reload, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Reload configuration");
c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable interception of a session");
cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable throttling of a session");
cli_register_command(cli, NULL, "filter", cmd_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Add filtering to a session");
cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
#ifdef BGP
c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP neighbour");
#endif /* BGP */
c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable interception of a session");
cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable throttling of a session");
cli_register_command(cli, c, "filter", cmd_no_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Remove filtering from a session");
cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
#ifdef BGP
c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP neighbour");
c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
c = cli_register_command(cli, NULL, "router", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
cli_register_command(cli, c, "bgp", cmd_router_bgp, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Configure BGP");
cli_register_command(cli, NULL, "neighbour", cmd_router_bgp_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Configure BGP neighbour");
c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, NULL);
cli_register_command(cli, c, "neighbour", cmd_router_bgp_no_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Remove BGP neighbour");
#endif /* BGP */
c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
c = cli_register_command(cli, NULL, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
cli_register_command(cli, c, "access-list", cmd_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Add named access-list");
cli_register_command(cli, NULL, "permit", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Permit rule");
cli_register_command(cli, NULL, "deny", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Deny rule");
c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_CONFIG, NULL);
c2 = cli_register_command(cli, c, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
cli_register_command(cli, c2, "access-list", cmd_no_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove named access-list");
// Enable regular processing
cli_regular(cli, regular_stuff);
if (!(f = fopen(CLIUSERS, "r")))
{
LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
}
else
{
while (fgets(buf, 4096, f))
{
char *p;
if (*buf == '#') continue;
if ((p = strchr(buf, '\r'))) *p = 0;
if ((p = strchr(buf, '\n'))) *p = 0;
if (!*buf) continue;
if (!(p = strchr((char *)buf, ':'))) continue;
*p++ = 0;
if (!strcmp(buf, "enable"))
{
cli_allow_enable(cli, p);
LOG(3, 0, 0, "Setting enable password\n");
}
else
{
cli_allow_user(cli, buf, p);
LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
}
}
fclose(f);
}
memset(&addr, 0, sizeof(addr));
clifd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
{
int flags;
// Set cli fd as non-blocking
flags = fcntl(clifd, F_GETFL, 0);
fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(23);
if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
{
LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
return;
}
listen(clifd, 10);
}
void cli_do(int sockfd)
{
int require_auth = 1;
struct sockaddr_in addr;
socklen_t l = sizeof(addr);
if (fork_and_close()) return;
if (getpeername(sockfd, (struct sockaddr *) &addr, &l) == 0)
{
require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
LOG(require_auth ? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
fmtaddr(addr.sin_addr.s_addr, 0));
}
else
LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
if (require_auth)
{
LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
if (!cli->users) /* paranoia */
{
LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
exit(0);
}
}
else
{
/* no username/pass required */
cli->users = 0;
}
#ifdef RINGBUFFER
debug_rb_tail = ringbuffer->tail;
#endif
memset(&debug_flags, 0, sizeof(debug_flags));
debug_flags.critical = 1;
cli_loop(cli, sockfd);
close(sockfd);
LOG(require_auth ? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
fmtaddr(addr.sin_addr.s_addr, 0));
exit(0);
}
static void cli_print_log(struct cli_def *cli, char *string)
{
LOG(3, 0, 0, "%s\n", string);
}
void cli_do_file(FILE *fh)
{
LOG(3, 0, 0, "Reading configuration file\n");
cli_print_callback(cli, cli_print_log);
cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
cli_print_callback(cli, NULL);
}
int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
{
va_list ap;
char *desc;
char buf[16];
char *p;
va_start(ap, entry);
while (entry)
{
/* allow one %d */
if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
{
int v = va_arg(ap, int);
snprintf(buf, sizeof(buf), entry, v);
p = buf;
}
else
p = entry;
desc = va_arg(ap, char *);
if (desc && *desc)
cli_error(cli, " %-20s %s", p, desc);
else
cli_error(cli, " %s", p);
entry = desc ? va_arg(ap, char *) : 0;
}
va_end(ap);
if (cr_ok)
cli_error(cli, " <cr>");
return CLI_OK;
}
static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
{
int i;
if (CLI_HELP_REQUESTED)
return cli_arg_help(cli, 1,
"<1-%d>", MAXSESSION-1, "Show specific session by id",
NULL);
time(&time_now);
if (argc > 0)
{
// Show individual session
for (i = 0; i < argc; i++)
{
unsigned int s, b_in, b_out;
s = atoi(argv[i]);
if (s <= 0 || s >= MAXSESSION)
{
cli_print(cli, "Invalid session id \"%s\"", argv[i]);
continue;
}
cli_print(cli, "\r\nSession %d:", s);
cli_print(cli, "\tUser:\t\t%s", session[s].user[0] ? session[s].user : "none");
cli_print(cli, "\tCalling Num:\t%s", session[s].calling);
cli_print(cli, "\tCalled Num:\t%s", session[s].called);
cli_print(cli, "\tTunnel ID:\t%d", session[s].tunnel);
cli_print(cli, "\tPPP Phase:\t%s", ppp_phase(session[s].ppp.phase));
switch (session[s].ppp.phase)
{
case Establish:
cli_print(cli, "\t LCP state:\t%s", ppp_state(session[s].ppp.lcp));
break;
case Authenticate:
case Network:
cli_print(cli, "\t IPCP state:\t%s", ppp_state(session[s].ppp.ipcp));
cli_print(cli, "\t IPV6CP state:\t%s", ppp_state(session[s].ppp.ipv6cp));
cli_print(cli, "\t CCP state:\t%s", ppp_state(session[s].ppp.ccp));
}
cli_print(cli, "\tIP address:\t%s", fmtaddr(htonl(session[s].ip), 0));
cli_print(cli, "\tUnique SID:\t%u", session[s].unique_id);
cli_print(cli, "\tOpened:\t\t%u seconds", session[s].opened ? abs(time_now - session[s].opened) : 0);
cli_print(cli, "\tIdle time:\t%u seconds", session[s].last_packet ? abs(time_now - session[s].last_packet) : 0);
if (session[s].session_timeout)
{
clockt opened = session[s].opened;
if (session[s].bundle && bundle[session[s].bundle].num_of_links > 1)
opened = bundle[session[s].bundle].online_time;
cli_print(cli, "\tSess Timeout:\t%u seconds", session[s].session_timeout - (opened ? abs(time_now - opened) : 0));
}
if (session[s].idle_timeout)
cli_print(cli, "\tIdle Timeout:\t%u seconds", session[s].idle_timeout - (session[s].last_data ? abs(time_now - session[s].last_data) : 0));
cli_print(cli, "\tBytes In/Out:\t%u/%u", session[s].cout, session[s].cin);
cli_print(cli, "\tPkts In/Out:\t%u/%u", session[s].pout, session[s].pin);
cli_print(cli, "\tMRU:\t\t%d", session[s].mru);
cli_print(cli, "\tRx Speed:\t%u", session[s].rx_connect_speed);
cli_print(cli, "\tTx Speed:\t%u", session[s].tx_connect_speed);
if (session[s].filter_in && session[s].filter_in <= MAXFILTER)
cli_print(cli, "\tFilter in:\t%u (%s)", session[s].filter_in, ip_filters[session[s].filter_in - 1].name);
if (session[s].filter_out && session[s].filter_out <= MAXFILTER)
cli_print(cli, "\tFilter out:\t%u (%s)", session[s].filter_out, ip_filters[session[s].filter_out - 1].name);
if (session[s].snoop_ip && session[s].snoop_port)
cli_print(cli, "\tIntercepted:\t%s:%d", fmtaddr(session[s].snoop_ip, 0), session[s] .snoop_port);
else
cli_print(cli, "\tIntercepted:\tno");
cli_print(cli, "\tWalled Garden:\t%s", session[s].walled_garden ? "YES" : "no");
{
int t = (session[s].throttle_in || session[s].throttle_out);
cli_print(cli, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
t ? "YES" : "no", t ? " (" : "",
session[s].throttle_in, session[s].throttle_in ? "kbps" : t ? "-" : "",
t ? "/" : "",
session[s].throttle_out, session[s].throttle_out ? "kbps" : t ? "-" : "",
t ? ")" : "");
}
b_in = session[s].tbf_in;
b_out = session[s].tbf_out;
if (b_in || b_out)
cli_print(cli, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
"Rate", "Credit", "Queued", "ByteIn", "PackIn",
"ByteSent", "PackSent", "PackDrop", "PackDelay");
if (b_in)
cli_print(cli, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
b_in,
(filter_list[b_in].next ? "*" : " "),
(b_in < 100 ? "\t" : ""),
filter_list[b_in].rate * 8,
filter_list[b_in].credit,
filter_list[b_in].queued,
filter_list[b_in].b_queued,
filter_list[b_in].p_queued,
filter_list[b_in].b_sent,
filter_list[b_in].p_sent,
filter_list[b_in].p_dropped,
filter_list[b_in].p_delayed);
if (b_out)
cli_print(cli, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
b_out,
(filter_list[b_out].next ? "*" : " "),
(b_out < 100 ? "\t" : ""),
filter_list[b_out].rate * 8,
filter_list[b_out].credit,
filter_list[b_out].queued,
filter_list[b_out].b_queued,
filter_list[b_out].p_queued,
filter_list[b_out].b_sent,
filter_list[b_out].p_sent,
filter_list[b_out].p_dropped,
filter_list[b_out].p_delayed);
}
return CLI_OK;
}
// Show Summary
cli_print(cli, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %-15s %s",
"SID",
"TID",
"Username",
"IP",
"I",
"T",
"G",
"6",
"opened",
"downloaded",
"uploaded",
"idle",
"LAC",
"CLI");
for (i = 1; i < MAXSESSION; i++)
{
if (!session[i].opened) continue;
cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %-15s %s",
i,
session[i].tunnel,
session[i].user[0] ? session[i].user : "*",
fmtaddr(htonl(session[i].ip), 0),
(session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
(session[i].throttle_in || session[i].throttle_out) ? "Y" : "N",
(session[i].walled_garden) ? "Y" : "N",
(session[i].ppp.ipv6cp == Opened) ? "Y" : "N",
abs(time_now - (unsigned long)session[i].opened),
(unsigned long)session[i].cout,
(unsigned long)session[i].cin,
abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
fmtaddr(htonl(tunnel[ session[i].tunnel ].ip), 1),
session[i].calling[0] ? session[i].calling : "*");
}
return CLI_OK;
}
static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
{
int i, x, show_all = 0;
char *states[] = {
"Free",
"Open",
"Closing",
"Opening",
};
if (CLI_HELP_REQUESTED)
{
if (argc > 1)
return cli_arg_help(cli, 1,
"<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
NULL);
return cli_arg_help(cli, 1,
"all", "Show all tunnels, including unused",
"<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
NULL);
}
time(&time_now);
if (argc > 0)
{
if (strcmp(argv[0], "all") == 0)
{
show_all = 1;
}
else
{
// Show individual tunnel
for (i = 0; i < argc; i++)
{
char s[65535] = {0};
unsigned int t;
t = atoi(argv[i]);
if (t <= 0 || t >= MAXTUNNEL)
{
cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
continue;
}
cli_print(cli, "\r\nTunnel %d:", t);
cli_print(cli, "\tState:\t\t%s", states[tunnel[t].state]);
cli_print(cli, "\tHostname:\t%s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
cli_print(cli, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel[t].ip), 0));
cli_print(cli, "\tRemote Port:\t%d", tunnel[t].port);
cli_print(cli, "\tRx Window:\t%u", tunnel[t].window);
cli_print(cli, "\tNext Recv:\t%u", tunnel[t].nr);
cli_print(cli, "\tNext Send:\t%u", tunnel[t].ns);
cli_print(cli, "\tQueue Len:\t%u", tunnel[t].controlc);
cli_print(cli, "\tLast Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
for (x = 0; x < MAXSESSION; x++)
if (session[x].tunnel == t && session[x].opened && !session[x].die)
sprintf(s, "%s%u ", s, x);
cli_print(cli, "\tSessions:\t%s", s);
}
return CLI_OK;
}
}
// Show tunnel summary
cli_print(cli, "%4s %20s %20s %6s %s",
"TID",
"Hostname",
"IP",
"State",
"Sessions");
for (i = 1; i < MAXTUNNEL; i++)
{
int sessions = 0;
if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
cli_print(cli, "%4d %20s %20s %6s %6d",
i,
*tunnel[i].hostname ? tunnel[i].hostname : "(null)",
fmtaddr(htonl(tunnel[i].ip), 0),
states[tunnel[i].state],
sessions);
}
return CLI_OK;
}
static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
{
char sid[32][8];
char *sargv[32];
int sargc = 0;
int i;
if (CLI_HELP_REQUESTED)
return cli_arg_help(cli, 1,
"USER", "Show details for specific username",
NULL);
for (i = 0; i < MAXSESSION; i++)
{
if (!session[i].opened) continue;
if (!session[i].user[0]) continue;
if (argc > 0)
{
int j;
for (j = 0; j < argc && sargc < 32; j++)
{
if (strcmp(argv[j], session[i].user) == 0)
{
snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
sargv[sargc] = sid[sargc];
sargc++;
}
}
continue;
}
cli_print(cli, "%s", session[i].user);
}
if (sargc > 0)
return cmd_show_session(cli, "users", sargv, sargc);
return CLI_OK;
}
#ifdef STATISTICS
static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
{
if (CLI_HELP_REQUESTED)
return CLI_HELP_NO_ARGS;
cli_print(cli, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
cli_print(cli, "%-10s %10u %10u %10u %10u", "RX",
GET_STAT(tun_rx_bytes),
GET_STAT(tun_rx_packets),
GET_STAT(tun_rx_errors),
GET_STAT(tun_rx_dropped));
cli_print(cli, "%-10s %10u %10u %10u", "TX",
GET_STAT(tun_tx_bytes),
GET_STAT(tun_tx_packets),
GET_STAT(tun_tx_errors));
cli_print(cli, "");
cli_print(cli, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
cli_print(cli, "%-10s %10u %10u %10u", "RX",
GET_STAT(tunnel_rx_bytes),
GET_STAT(tunnel_rx_packets),
GET_STAT(tunnel_rx_errors));
cli_print(cli, "%-10s %10u %10u %10u %10u", "TX",
GET_STAT(tunnel_tx_bytes),
GET_STAT(tunnel_tx_packets),
GET_STAT(tunnel_tx_errors),
GET_STAT(tunnel_retries));
cli_print(cli, "");
cli_print(cli, "%-30s%-10s", "Counter", "Value");
cli_print(cli, "-----------------------------------------");
cli_print(cli, "%-30s%u", "radius_retries", GET_STAT(radius_retries));
cli_print(cli, "%-30s%u", "arp_sent", GET_STAT(arp_sent));
cli_print(cli, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped));
cli_print(cli, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created));
cli_print(cli, "%-30s%u", "session_created", GET_STAT(session_created));
cli_print(cli, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout));
cli_print(cli, "%-30s%u", "session_timeout", GET_STAT(session_timeout));
cli_print(cli, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout));
cli_print(cli, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow));
cli_print(cli, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow));
cli_print(cli, "%-30s%u", "session_overflow", GET_STAT(session_overflow));
cli_print(cli, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated));
cli_print(cli, "%-30s%u", "ip_freed", GET_STAT(ip_freed));
cli_print(cli, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded));
cli_print(cli, "%-30s%u", "recv_forward", GET_STAT(recv_forward));
cli_print(cli, "%-30s%u", "select_called", GET_STAT(select_called));
cli_print(cli, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used));
cli_print(cli, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded));
#ifdef STAT_CALLS
cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
cli_print(cli, "-----------------------------------------");
cli_print(cli, "%-30s%u", "call_processtun", GET_STAT(call_processtun));
cli_print(cli, "%-30s%u", "call_processipout", GET_STAT(call_processipout));
cli_print(cli, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out));
cli_print(cli, "%-30s%u", "call_processudp", GET_STAT(call_processudp));
cli_print(cli, "%-30s%u", "call_processpap", GET_STAT(call_processpap));
cli_print(cli, "%-30s%u", "call_processchap", GET_STAT(call_processchap));
cli_print(cli, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp));
cli_print(cli, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp));
cli_print(cli, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp));
cli_print(cli, "%-30s%u", "call_processipin", GET_STAT(call_processipin));
cli_print(cli, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in));
cli_print(cli, "%-30s%u", "call_processccp", GET_STAT(call_processccp));
cli_print(cli, "%-30s%u", "call_processrad", GET_STAT(call_processrad));
cli_print(cli, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp));
cli_print(cli, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp));
cli_print(cli, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap));
cli_print(cli, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip));
cli_print(cli, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6));
cli_print(cli, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
cli_print(cli, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend));
cli_print(cli, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill));
cli_print(cli, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
cli_print(cli, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill));
cli_print(cli, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
cli_print(cli, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup));
cli_print(cli, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address));
cli_print(cli, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address));
cli_print(cli, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
cli_print(cli, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend));
cli_print(cli, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry));
cli_print(cli, "%-30s%u", "call_random_data", GET_STAT(call_random_data));
#endif /* STAT_CALLS */
{
time_t l = GET_STAT(last_reset);
char *t = ctime(&l);
char *p = strchr(t, '\n');
if (p) *p = 0;
cli_print(cli, "");
cli_print(cli, "Last counter reset %s", t);
}
return CLI_OK;
}
static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
{
if (CLI_HELP_REQUESTED)
return CLI_HELP_NO_ARGS;
memset(_statistics, 0, sizeof(struct Tstats));
SET_STAT(last_reset, time(NULL));
cli_print(cli, "Counters cleared");
return CLI_OK;
}
#endif /* STATISTICS */
static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
{
if (CLI_HELP_REQUESTED)
return CLI_HELP_NO_ARGS;
cli_print(cli, "L2TPNS %s", VERSION);
return CLI_OK;
}
static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
{
int i;
int used = 0, free = 0, show_all = 0;
if (!config->cluster_iam_master)
{
cli_print(cli, "Can't do this on a slave. Do it on %s",
fmtaddr(config->cluster_master_address, 0));
return CLI_OK;
}
if (CLI_HELP_REQUESTED)
{
if (argc > 1)
return cli_arg_help(cli, 1, NULL);
return cli_arg_help(cli, 1,
"all", "Show all pool addresses, including unused",
NULL);
}
if (argc > 0 && strcmp(argv[0], "all") == 0)
show_all = 1;
time(&time_now);
cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
for (i = 0; i < MAXIPPOOL; i++)
{
if (!ip_address_pool[i].address) continue;
if (ip_address_pool[i].assigned)
{
cli_print(cli, "%-15s\tY %8d %s",
fmtaddr(htonl(ip_address_pool[i].address), 0),
ip_address_pool[i].session,
session[ip_address_pool[i].session].user);
used++;
}
else
{
if (ip_address_pool[i].last)
cli_print(cli, "%-15s\tN %8s [%s] %ds",
fmtaddr(htonl(ip_address_pool[i].address), 0), "",
ip_address_pool[i].user, (int) time_now - ip_address_pool[i].last);
else if (show_all)
cli_print(cli, "%-15s\tN", fmtaddr(htonl(ip_address_pool[i].address), 0));
free++;
}
}
if (!show_all)
cli_print(cli, "(Not displaying unused addresses)");
cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
return CLI_OK;
}
static FILE *save_config_fh = 0;
static void print_save_config(struct cli_def *cli, char *string)
{
if (save_config_fh)
fprintf(save_config_fh, "%s\n", string);
}
static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
{
if (CLI_HELP_REQUESTED)
return CLI_HELP_NO_ARGS;
if ((save_config_fh = fopen(config->config_file, "w")))
{
cli_print(cli, "Writing configuration");
cli_print_callback(cli, print_save_config);
cmd_show_run(cli, command, argv, argc);
cli_print_callback(cli, NULL);
fclose(save_config_fh);
save_config_fh = 0;
}
else
{
cli_error(cli, "Error writing configuration: %s", strerror(errno));
}
return CLI_OK;
}
static char const *show_access_list_rule(int extended, ip_filter_rulet *rule);
static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
{
int i;
char ipv6addr[INET6_ADDRSTRLEN];
if (CLI_HELP_REQUESTED)
return CLI_HELP_NO_ARGS;
cli_print(cli, "# Current configuration:");
for (i = 0; config_values[i].key; i++)
{
void *value = ((void *)config) + config_values[i].offset;
if (config_values[i].type == STRING)
cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *) value);
else if (config_values[i].type == IPv4)
cli_print(cli, "set %s %s", config_values[i].key, fmtaddr(*(in_addr_t *) value, 0));
else if (config_values[i].type == IPv6)
cli_print(cli, "set %s %s", config_values[i].key, inet_ntop(AF_INET6, value, ipv6addr, INET6_ADDRSTRLEN));
else if (config_values[i].type == SHORT)
cli_print(cli, "set %s %hu", config_values[i].key, *(short *) value);
else if (config_values[i].type == BOOL)
cli_print(cli, "set %s %s", config_values[i].key, (*(int *) value) ? "yes" : "no");
else if (config_values[i].type == INT)
cli_print(cli, "set %s %d", config_values[i].key, *(int *) value);
else if (config_values[i].type == UNSIGNED_LONG)
cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *) value);
}
cli_print(cli, "# Plugins");
for (i = 0; i < MAXPLUGINS; i++)
{
if (*config->plugins[i])
{
cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
}
}
#ifdef BGP
if (config->as_number)
{
int k;
int h;
cli_print(cli, "# BGP");
cli_print(cli, "router bgp %u", config->as_number);
for (i = 0; i < BGP_NUM_PEERS; i++)
{
if (!config->neighbour[i].name[0])
continue;
cli_print(cli, " neighbour %s remote-as %u", config->neighbour[i].name, config->neighbour[i].as);
k = config->neighbour[i].keepalive;
h = config->neighbour[i].hold;
if (k == -1)
{
if (h == -1)
continue;
k = BGP_KEEPALIVE_TIME;
}
if (h == -1)
h = BGP_HOLD_TIME;
cli_print(cli, " neighbour %s timers %d %d", config->neighbour[i].name, k, h);
}
}
#endif
cli_print(cli, "# Filters");
for (i = 0; i < MAXFILTER; i++)
{
ip_filter_rulet *rules;
if (!*ip_filters[i].name)
continue;
cli_print(cli, "ip access-list %s %s",
ip_filters[i].extended ? "extended" : "standard",
ip_filters[i].name);
rules = ip_filters[i].rules;
while (rules->action)
cli_print(cli, "%s", show_access_list_rule(ip_filters[i].extended, rules++));
}
cli_print(cli, "# end");
return CLI_OK;
}
static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
{
int i, free = 0, used = 0, show_all = 0;
char *states[] = {
"NULL",
"CHAP",
"AUTH",
"IPCP",
"START",
"STOP",
"INTRM",
"WAIT",
};
if (CLI_HELP_REQUESTED)
{
if (argc > 1)