update for new clustering
[l2tpns.git] / cli.c
1 // L2TPNS Command Line Interface
2 // vim: sw=4 ts=8
3
4 char const *cvs_name = "$Name: $";
5 char const *cvs_id_cli = "$Id: cli.c,v 1.8 2004-07-07 09:09:53 bodea Exp $";
6
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <sys/file.h>
10 #include <sys/stat.h>
11 #include <syslog.h>
12 #include <malloc.h>
13 #include <sched.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <dlfcn.h>
25 #include <libcli.h>
26 #include "l2tpns.h"
27 #include "util.h"
28 #include "cluster.h"
29 #include "tbf.h"
30 #include "ll.h"
31 #ifdef BGP
32 #include "bgp.h"
33 #endif
34
35 extern tunnelt *tunnel;
36 extern sessiont *session;
37 extern radiust *radius;
38 extern ippoolt *ip_address_pool;
39 extern struct Tstats *_statistics;
40 struct cli_def *cli = NULL;
41 int cli_quit = 0;
42 extern int clifd, udpfd, tapfd, snoopfd, ifrfd, cluster_sockfd;
43 extern int *radfds;
44 extern sessionidt *cli_session_kill;
45 extern tunnelidt *cli_tunnel_kill;
46 extern struct configt *config;
47 extern struct config_descriptt config_values[];
48 #ifdef RINGBUFFER
49 extern struct Tringbuffer *ringbuffer;
50 #endif
51
52 char *debug_levels[] = {
53 "CRIT",
54 "ERROR",
55 "WARN",
56 "INFO",
57 "CALL",
58 "DATA",
59 };
60
61 struct
62 {
63 char critical;
64 char error;
65 char warning;
66 char info;
67 char calls;
68 char data;
69 } debug_flags;
70
71 int debug_session;
72 int debug_tunnel;
73 int debug_rb_tail;
74 FILE *save_config_fh;
75
76 int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
77 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
78 int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
79 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
80 int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
81 int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
82 int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
83 int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
84 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
85 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
86 int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc);
87 int cmd_show_cluster(struct cli_def *cli, char *command, char **argv, int argc);
88 int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
89 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
90 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
91 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
92 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
93 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
94 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
95 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
96 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
97 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
98 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
99 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
100 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
101 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
102 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
103 int regular_stuff(struct cli_def *cli);
104
105 void init_cli(char *hostname)
106 {
107 FILE *f;
108 char buf[4096];
109 struct cli_command *c;
110 struct cli_command *c2;
111 int on = 1;
112 struct sockaddr_in addr;
113
114 cli = cli_init();
115 if (hostname && *hostname)
116 cli_set_hostname(cli, hostname);
117 else
118 cli_set_hostname(cli, "l2tpns");
119
120 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
121 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
122 #ifdef BGP
123 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
124 #endif /* BGP */
125 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
126 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
127 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
128 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
129 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
130 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the currently running configuration");
131 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
132 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
133 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
134 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
135 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");
136 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
137
138 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
139 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
140 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
141
142 #ifdef STATISTICS
143 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
144
145 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
146 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
147 #endif
148
149 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
150
151 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
152 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
153 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
154
155 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable interception for a user");
156 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable throttling for a user");
157 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
158
159 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
160 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP peer");
161
162 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
163 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable interception for a user");
164 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable throttling for a user");
165 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
166 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
167 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP peer");
168
169 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
170 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
171 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
172 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
173
174 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
175 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
176
177 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
178 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
179
180 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
181 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
182
183 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
184
185 // Enable regular processing
186 cli_regular(cli, regular_stuff);
187
188 if (!(f = fopen(CLIUSERS, "r")))
189 {
190 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
191 }
192 else
193 {
194 while (fgets(buf, 4096, f))
195 {
196 char *p;
197 if (*buf == '#') continue;
198 if ((p = strchr(buf, '\r'))) *p = 0;
199 if ((p = strchr(buf, '\n'))) *p = 0;
200 if (!*buf) continue;
201 if (!(p = strchr((char *)buf, ':'))) continue;
202 *p++ = 0;
203 if (!strcmp(buf, "enable"))
204 {
205 cli_allow_enable(cli, p);
206 log(3, 0, 0, 0, "Setting enable password\n");
207 }
208 else
209 {
210 cli_allow_user(cli, buf, p);
211 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
212 }
213 }
214 fclose(f);
215 }
216
217 memset(&addr, 0, sizeof(addr));
218 clifd = socket(PF_INET, SOCK_STREAM, 6);
219 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
220 {
221 int flags;
222 // Set cli fd as non-blocking
223 flags = fcntl(clifd, F_GETFL, 0);
224 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
225 }
226 addr.sin_family = AF_INET;
227 addr.sin_port = htons(23);
228 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
229 {
230 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
231 return;
232 }
233 listen(clifd, 10);
234 }
235
236 void cli_do(int sockfd)
237 {
238 int i;
239
240 if (fork()) return;
241 if (config->scheduler_fifo)
242 {
243 int ret;
244 struct sched_param params = {0};
245 params.sched_priority = 0;
246 if ((ret = sched_setscheduler(0, SCHED_OTHER, &params)) == 0)
247 {
248 log(3, 0, 0, 0, "Dropped FIFO scheduler\n");
249 }
250 else
251 {
252 log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno));
253 log(0, 0, 0, 0, "This is probably really really bad.\n");
254 }
255 }
256
257 signal(SIGPIPE, SIG_DFL);
258 signal(SIGCHLD, SIG_DFL);
259 signal(SIGHUP, SIG_DFL);
260 signal(SIGUSR1, SIG_DFL);
261 signal(SIGQUIT, SIG_DFL);
262 signal(SIGKILL, SIG_DFL);
263 signal(SIGALRM, SIG_DFL);
264 signal(SIGTERM, SIG_DFL);
265
266 // Close sockets
267 if (udpfd) close(udpfd); udpfd = 0;
268 if (tapfd) close(tapfd); tapfd = 0;
269 if (snoopfd) close(snoopfd); snoopfd = 0;
270 for (i = 0; i < config->num_radfds; i++)
271 if (radfds[i]) close(radfds[i]);
272 if (ifrfd) close(ifrfd); ifrfd = 0;
273 if (cluster_sockfd) close(cluster_sockfd); cluster_sockfd = 0;
274 if (clifd) close(clifd); clifd = 0;
275 #ifdef BGP
276 for (i = 0; i < BGP_NUM_PEERS; i++)
277 if (bgp_peers[i].sock != -1)
278 close(bgp_peers[i].sock);
279 #endif /* BGP */
280
281 {
282 int require_auth = 1;
283 struct sockaddr_in addr;
284 int l = sizeof(addr);
285 if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0)
286 {
287 log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr.sin_addr.s_addr));
288 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
289 }
290 else
291 log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
292
293 if (require_auth)
294 {
295 log(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
296 if (!cli->users) /* paranoia */
297 {
298 log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
299 exit(0);
300 }
301 }
302 else
303 {
304 /* no username/pass required */
305 cli->users = 0;
306 }
307 }
308
309 debug_session = 0;
310 debug_tunnel = 0;
311 #ifdef RINGBUFFER
312 debug_rb_tail = ringbuffer->tail;
313 #endif
314 memset(&debug_flags, 0, sizeof(debug_flags));
315 debug_flags.critical = 1;
316
317 {
318 char prompt[1005];
319 snprintf(prompt, 1005, "l2tpns> ");
320 cli_loop(cli, sockfd, prompt);
321 }
322
323 close(sockfd);
324 log(3, 0, 0, 0, "Closed CLI connection\n");
325 exit(0);
326 }
327
328 void cli_print_log(struct cli_def *cli, char *string)
329 {
330 log(3, 0, 0, 0, "%s\n", string);
331 }
332
333 void cli_do_file(FILE *fh)
334 {
335 log(3, 0, 0, 0, "Reading configuration file\n");
336 cli_print_callback(cli, cli_print_log);
337 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
338 cli_print_callback(cli, NULL);
339 }
340
341 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
342 {
343 va_list ap;
344 char *desc;
345 char buf[16];
346 char *p;
347
348 va_start(ap, entry);
349 while (entry)
350 {
351 /* allow one %d */
352 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
353 {
354 int v = va_arg(ap, int);
355 snprintf(buf, sizeof(buf), entry, v);
356 p = buf;
357 }
358 else
359 p = entry;
360
361 desc = va_arg(ap, char *);
362 if (desc && *desc)
363 cli_print(cli, " %-20s %s", p, desc);
364 else
365 cli_print(cli, " %s", p);
366
367 entry = desc ? va_arg(ap, char *) : 0;
368 }
369
370 va_end(ap);
371 if (cr_ok)
372 cli_print(cli, " <cr>");
373
374 return CLI_OK;
375 }
376
377 int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
378 {
379 int i;
380
381 if (CLI_HELP_REQUESTED)
382 return cli_arg_help(cli, 1,
383 "<1-%d>", MAXSESSION-1, "Show specific session by id",
384 NULL);
385
386 time(&time_now);
387 if (argc > 0)
388 {
389 // Show individual session
390 for (i = 0; i < argc; i++)
391 {
392 unsigned int s;
393 s = atoi(argv[i]);
394 if (s <= 0 || s >= MAXSESSION)
395 {
396 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
397 continue;
398 }
399 cli_print(cli, "\r\nSession %d:", s);
400 cli_print(cli, " User: %s", session[s].user[0] ? session[s].user : "none");
401 cli_print(cli, " Calling Num: %s", session[s].calling);
402 cli_print(cli, " Called Num: %s", session[s].called);
403 cli_print(cli, " Tunnel ID: %d", session[s].tunnel);
404 cli_print(cli, " IP address: %s", inet_toa(htonl(session[s].ip)));
405 cli_print(cli, " HSD sid: %lu", session[s].sid);
406 cli_print(cli, " Idle time: %u seconds", abs(time_now - session[s].last_packet));
407 cli_print(cli, " Next Recv: %u", session[s].nr);
408 cli_print(cli, " Next Send: %u", session[s].ns);
409 cli_print(cli, " Bytes In/Out: %lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin);
410 cli_print(cli, " Pkts In/Out: %lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin);
411 cli_print(cli, " MRU: %d", session[s].mru);
412 cli_print(cli, " Radius Session: %u", session[s].radius);
413 cli_print(cli, " Rx Speed: %lu", session[s].rx_connect_speed);
414 cli_print(cli, " Tx Speed: %lu", session[s].tx_connect_speed);
415 if (session[s].snoop_ip && session[s].snoop_port)
416 cli_print(cli, " Intercepted: %s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port);
417 else
418 cli_print(cli, " Intercepted: no");
419 cli_print(cli, " Throttled: %s", session[s].throttle ? "YES" : "no");
420 cli_print(cli, " Walled Garden: %s", session[s].walled_garden ? "YES" : "no");
421 cli_print(cli, " Filter BucketI: %d", session[s].tbf_in);
422 cli_print(cli, " Filter BucketO: %d", session[s].tbf_out);
423 }
424 return CLI_OK;
425 }
426
427 // Show Summary
428 cli_print(cli, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
429 "SID",
430 "TID",
431 "Username",
432 "IP",
433 "I",
434 "T",
435 "G",
436 "opened",
437 "downloaded",
438 "uploaded",
439 "idle",
440 "LAC",
441 "CLI");
442 for (i = 1; i < MAXSESSION; i++)
443 {
444 char *userip, *tunnelip;
445 if (!session[i].opened) continue;
446 userip = strdup(inet_toa(htonl(session[i].ip)));
447 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
448 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
449 i,
450 session[i].tunnel,
451 session[i].user[0] ? session[i].user : "*",
452 userip,
453 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
454 (session[i].throttle) ? "Y" : "N",
455 (session[i].walled_garden) ? "Y" : "N",
456 abs(time_now - (unsigned long)session[i].opened),
457 (unsigned long)session[i].total_cout,
458 (unsigned long)session[i].total_cin,
459 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
460 tunnelip,
461 session[i].calling[0] ? session[i].calling : "*");
462 if (userip) free(userip);
463 if (tunnelip) free(tunnelip);
464 }
465 return CLI_OK;
466 }
467
468 int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
469 {
470 int i, x, show_all = 0;
471 char *states[] = {
472 "Free",
473 "Open",
474 "Closing",
475 "Opening",
476 };
477
478 if (CLI_HELP_REQUESTED)
479 {
480 if (argc > 1)
481 return cli_arg_help(cli, 1,
482 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
483 NULL);
484
485 return cli_arg_help(cli, 1,
486 "all", "Show all tunnels, including unused",
487 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
488 NULL);
489 }
490
491 time(&time_now);
492 if (argc > 0)
493 {
494 if (strcmp(argv[0], "all") == 0)
495 {
496 show_all = 1;
497 }
498 else
499 {
500 // Show individual tunnel
501 for (i = 0; i < argc; i++)
502 {
503 char s[65535] = {0};
504 unsigned int t;
505 t = atoi(argv[i]);
506 if (t <= 0 || t >= MAXTUNNEL)
507 {
508 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
509 continue;
510 }
511 cli_print(cli, "\r\nTunnel %d:", t);
512 cli_print(cli, " State: %s", states[tunnel[t].state]);
513 cli_print(cli, " Hostname: %s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
514 cli_print(cli, " Remote IP: %s", inet_toa(htonl(tunnel[t].ip)));
515 cli_print(cli, " Remote Port: %d", tunnel[t].port);
516 cli_print(cli, " Rx Window: %u", tunnel[t].window);
517 cli_print(cli, " Next Recv: %u", tunnel[t].nr);
518 cli_print(cli, " Next Send: %u", tunnel[t].ns);
519 cli_print(cli, " Queue Len: %u", tunnel[t].controlc);
520 cli_print(cli, " Last Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
521
522 for (x = 0; x < MAXSESSION; x++)
523 if (session[x].tunnel == t && session[x].opened && !session[x].die)
524 sprintf(s, "%s%u ", s, x);
525 cli_print(cli, " Sessions: %s", s);
526 }
527 return CLI_OK;
528 }
529 }
530
531 // Show tunnel summary
532 cli_print(cli, "%4s %20s %20s %6s %s",
533 "TID",
534 "Hostname",
535 "IP",
536 "State",
537 "Sessions");
538 for (i = 1; i < MAXTUNNEL; i++)
539 {
540 int sessions = 0;
541 if (!show_all && (!tunnel[i].ip || tunnel[i].die || !tunnel[i].hostname[0])) continue;
542
543 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
544 cli_print(cli, "%4d %20s %20s %6s %6d",
545 i,
546 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
547 inet_toa(htonl(tunnel[i].ip)),
548 states[tunnel[i].state],
549 sessions);
550 }
551 return CLI_OK;
552 }
553
554 int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
555 {
556 char sid[32][8];
557 char *sargv[32];
558 int sargc = 0;
559 int i;
560
561 if (CLI_HELP_REQUESTED)
562 return cli_arg_help(cli, 1,
563 "USER", "Show details for specific username",
564 NULL);
565
566 for (i = 0; i < MAXSESSION; i++)
567 {
568 if (!session[i].opened) continue;
569 if (!session[i].user[0]) continue;
570 if (argc > 0)
571 {
572 int j;
573 for (j = 0; j < argc && sargc < 32; j++)
574 {
575 if (strcmp(argv[j], session[i].user) == 0)
576 {
577 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
578 sargv[sargc] = sid[sargc];
579 sargc++;
580 }
581 }
582
583 continue;
584 }
585
586 cli_print(cli, "%s", session[i].user);
587 }
588
589 if (sargc > 0)
590 return cmd_show_session(cli, "users", sargv, sargc);
591
592 return CLI_OK;
593 }
594
595 int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
596 {
597 if (CLI_HELP_REQUESTED)
598 return CLI_HELP_NO_ARGS;
599
600 cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
601 cli_print(cli, "%-10s %8lu %8lu %8lu", "RX",
602 GET_STAT(tap_rx_bytes),
603 GET_STAT(tap_rx_packets),
604 GET_STAT(tap_rx_errors));
605 cli_print(cli, "%-10s %8lu %8lu %8lu", "TX",
606 GET_STAT(tap_tx_bytes),
607 GET_STAT(tap_tx_packets),
608 GET_STAT(tap_tx_errors));
609 cli_print(cli, "");
610
611 cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
612 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX",
613 GET_STAT(tunnel_rx_bytes),
614 GET_STAT(tunnel_rx_packets),
615 GET_STAT(tunnel_rx_errors),
616 0L);
617 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX",
618 GET_STAT(tunnel_tx_bytes),
619 GET_STAT(tunnel_tx_packets),
620 GET_STAT(tunnel_tx_errors),
621 GET_STAT(tunnel_retries));
622 cli_print(cli, "");
623
624 cli_print(cli, "%-30s%-10s", "Counter", "Value");
625 cli_print(cli, "-----------------------------------------");
626 cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries));
627 cli_print(cli, "%-30s%lu", "arp_errors", GET_STAT(arp_errors));
628 cli_print(cli, "%-30s%lu", "arp_replies", GET_STAT(arp_replies));
629 cli_print(cli, "%-30s%lu", "arp_discarded", GET_STAT(arp_discarded));
630 cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent));
631 cli_print(cli, "%-30s%lu", "arp_recv", GET_STAT(arp_recv));
632 cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped));
633 cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created));
634 cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created));
635 cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout));
636 cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout));
637 cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout));
638 cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow));
639 cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow));
640 cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow));
641 cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated));
642 cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed));
643 cli_print(cli, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded));
644 cli_print(cli, "%-30s%lu", "recv_forward", GET_STAT(recv_forward));
645
646
647 #ifdef STATISTICS
648 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
649 cli_print(cli, "-----------------------------------------");
650 cli_print(cli, "%-30s%lu", "call_processtap", GET_STAT(call_processtap));
651 cli_print(cli, "%-30s%lu", "call_processarp", GET_STAT(call_processarp));
652 cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout));
653 cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp));
654 cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap));
655 cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap));
656 cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp));
657 cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp));
658 cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin));
659 cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp));
660 cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad));
661 cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp));
662 cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp));
663 cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap));
664 cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip));
665 cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
666 cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend));
667 cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill));
668 cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
669 cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill));
670 cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
671 cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup));
672 cli_print(cli, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address));
673 cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address));
674 cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
675 cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend));
676 cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry));
677 #endif
678 return CLI_OK;
679 }
680
681 int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
682 {
683 int tag = 0;
684 int file = 0;
685 int i = 0;
686
687 if (CLI_HELP_REQUESTED)
688 return cli_arg_help(cli, 1,
689 "tag", "Include CVS release tag",
690 "file", "Include file versions",
691 NULL);
692
693 for (i = 0; i < argc; i++)
694 if (!strcmp(argv[i], "tag"))
695 tag++;
696 else if (!strcmp(argv[i], "file"))
697 file++;
698
699 cli_print(cli, "L2TPNS %s", VERSION);
700 if (tag)
701 {
702 char const *p = strchr(cvs_name, ':');
703 char const *e;
704 if (p)
705 {
706 p++;
707 while (isspace(*p))
708 p++;
709 }
710
711 if (!p || *p == '$')
712 p = "HEAD";
713
714 e = strpbrk(p, " \t$");
715 cli_print(cli, "Tag: %.*s", e ? e - p + 1 : strlen(p), p);
716 }
717
718 if (file)
719 {
720 extern linked_list *loaded_plugins;
721 void *p;
722
723 cli_print(cli, "Files:");
724 cli_print(cli, " %s", cvs_id_arp);
725 #ifdef BGP
726 cli_print(cli, " %s", cvs_id_bgp);
727 #endif /* BGP */
728 cli_print(cli, " %s", cvs_id_cli);
729 cli_print(cli, " %s", cvs_id_cluster);
730 cli_print(cli, " %s", cvs_id_constants);
731 cli_print(cli, " %s", cvs_id_control);
732 cli_print(cli, " %s", cvs_id_icmp);
733 cli_print(cli, " %s", cvs_id_l2tpns);
734 cli_print(cli, " %s", cvs_id_ll);
735 cli_print(cli, " %s", cvs_id_md5);
736 cli_print(cli, " %s", cvs_id_ppp);
737 cli_print(cli, " %s", cvs_id_radius);
738 cli_print(cli, " %s", cvs_id_tbf);
739 cli_print(cli, " %s", cvs_id_util);
740
741 ll_reset(loaded_plugins);
742 while ((p = ll_next(loaded_plugins)))
743 {
744 char const **id = dlsym(p, "cvs_id");
745 if (id)
746 cli_print(cli, " %s", *id);
747 }
748 }
749
750 return CLI_OK;
751 }
752
753 int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
754 {
755 int i;
756 int used = 0, free = 0, show_all = 0;
757
758 if (!config->cluster_iam_master)
759 {
760 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
761 return CLI_OK;
762 }
763
764 if (CLI_HELP_REQUESTED)
765 {
766 if (argc > 1)
767 return cli_arg_help(cli, 1, NULL);
768
769 return cli_arg_help(cli, 1,
770 "all", "Show all pool addresses, including unused",
771 NULL);
772 }
773
774 if (argc > 0 && strcmp(argv[0], "all") == 0)
775 show_all = 1;
776
777 time(&time_now);
778 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
779 for (i = 0; i < MAXIPPOOL; i++)
780 {
781 if (!ip_address_pool[i].address) continue;
782 if (ip_address_pool[i].assigned)
783 {
784 cli_print(cli, "%-15s Y %8d %s",
785 inet_toa(htonl(ip_address_pool[i].address)), ip_address_pool[i].session, session[ip_address_pool[i].session].user);
786
787 used++;
788 }
789 else
790 {
791 if (ip_address_pool[i].last)
792 cli_print(cli, "%-15s N %8s [%s] %ds",
793 inet_toa(htonl(ip_address_pool[i].address)), "",
794 ip_address_pool[i].user, time_now - ip_address_pool[i].last);
795 else if (show_all)
796 cli_print(cli, "%-15s N", inet_toa(htonl(ip_address_pool[i].address)));
797
798 free++;
799 }
800 }
801
802 if (!show_all)
803 cli_print(cli, "(Not displaying unused addresses)");
804
805 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
806 return CLI_OK;
807 }
808
809 void print_save_config(struct cli_def *cli, char *string)
810 {
811 if (save_config_fh)
812 fprintf(save_config_fh, "%s\n", string);
813 }
814
815 int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
816 {
817 if (CLI_HELP_REQUESTED)
818 return CLI_HELP_NO_ARGS;
819
820 if ((save_config_fh = fopen(config->config_file, "w")))
821 {
822 cli_print(cli, "Writing configuration");
823 cli_print_callback(cli, print_save_config);
824 cmd_show_run(cli, command, argv, argc);
825 cli_print_callback(cli, NULL);
826 fclose(save_config_fh);
827 }
828 else
829 {
830 cli_print(cli, "Error writing configuration: %s", strerror(errno));
831 }
832 return CLI_OK;
833 }
834
835 int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
836 {
837 int i;
838
839 if (CLI_HELP_REQUESTED)
840 return CLI_HELP_NO_ARGS;
841
842 cli_print(cli, "# Current configuration:");
843
844 for (i = 0; config_values[i].key; i++)
845 {
846 void *value = ((void *)config) + config_values[i].offset;
847 if (config_values[i].type == STRING)
848 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value);
849 else if (config_values[i].type == IP)
850 cli_print(cli, "set %s %s", config_values[i].key, inet_toa(*(unsigned *)value));
851 else if (config_values[i].type == SHORT)
852 cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value);
853 else if (config_values[i].type == BOOL)
854 cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no");
855 else if (config_values[i].type == INT)
856 cli_print(cli, "set %s %d", config_values[i].key, *(int *)value);
857 else if (config_values[i].type == UNSIGNED_LONG)
858 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value);
859 }
860
861 cli_print(cli, "# Plugins");
862 for (i = 0; i < MAXPLUGINS; i++)
863 {
864 if (*config->plugins[i])
865 {
866 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
867 }
868 }
869
870 cli_print(cli, "# end");
871 return CLI_OK;
872 }
873
874 int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
875 {
876 int i, free = 0, used = 0, show_all = 0;
877 char *states[] = {
878 "NULL",
879 "CHAP",
880 "AUTH",
881 "IPCP",
882 "START",
883 "STOP",
884 "WAIT",
885 };
886
887 if (CLI_HELP_REQUESTED)
888 {
889 if (argc > 1)
890 return cli_arg_help(cli, 1, NULL);
891
892 return cli_arg_help(cli, 1,
893 "all", "Show all RADIUS sessions, including unused",
894 NULL);
895 }
896
897 cli_print(cli, "%6s%5s%6s%9s%9s%4s", "Radius", "Sock", "State", "Session", "Retry", "Try");
898
899 time(&time_now);
900
901 if (argc > 0 && strcmp(argv[0], "all") == 0)
902 show_all = 1;
903
904 for (i = 1; i < MAXRADIUS; i++)
905 {
906 if (radius[i].state == RADIUSNULL)
907 free++;
908 else
909 used++;
910
911 if (!show_all && radius[i].state == RADIUSNULL) continue;
912
913 cli_print(cli, "%6d%5d%6s%9d%9u%4d",
914 i >> RADIUS_SHIFT,
915 i & RADIUS_MASK,
916 states[radius[i].state],
917 radius[i].session,
918 radius[i].retry,
919 radius[i].try);
920 }
921
922 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
923
924 return CLI_OK;
925 }
926
927 int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
928 {
929 int i;
930
931 if (CLI_HELP_REQUESTED)
932 return CLI_HELP_NO_ARGS;
933
934 cli_print(cli, "Plugins currently loaded:");
935 for (i = 0; i < MAXPLUGINS; i++)
936 {
937 if (*config->plugins[i])
938 {
939 cli_print(cli, " %s", config->plugins[i]);
940 }
941 }
942 return CLI_OK;
943 }
944
945 int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc)
946 {
947 int i;
948
949 if (CLI_HELP_REQUESTED)
950 return CLI_HELP_NO_ARGS;
951
952 cli_print(cli, "Token bucket filters:");
953 cli_print(cli, "%-6s %8s %-4s", "ID", "Handle", "Used");
954 for (i = 0; i < MAXSESSION; i++)
955 {
956 if (!session[i].throttle)
957 continue;
958
959 cli_print(cli, "%-6d %8d %8d",
960 i,
961 session[i].tbf_in,
962 session[i].tbf_out);
963 }
964 return CLI_OK;
965 }
966
967 int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
968 {
969 if (CLI_HELP_REQUESTED)
970 return CLI_HELP_NO_ARGS;
971
972 cli_print(cli, " _\n"
973 "//\\\n"
974 "V \\\n"
975 " \\ \\_\n"
976 " \\,'.`-.\n"
977 " |\\ `. `.\n"
978 " ( \\ `. `-. _,.-:\\\n"
979 " \\ \\ `. `-._ __..--' ,-';/\n"
980 " \\ `. `-. `-..___..---' _.--' ,'/\n"
981 " `. `. `-._ __..--' ,' /\n"
982 " `. `-_ ``--..'' _.-' ,'\n"
983 " `-_ `-.___ __,--' ,'\n"
984 " `-.__ `----\"\"\" __.-'\n"
985 "hh `--..____..--'");
986
987 return CLI_OK;
988 }
989
990 int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
991 {
992 if (CLI_HELP_REQUESTED)
993 return CLI_HELP_NO_ARGS;
994
995 cli_print(cli, "Counters cleared");
996 SET_STAT(last_reset, time(NULL));
997 return CLI_OK;
998 }
999
1000 int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
1001 {
1002 int i;
1003 sessionidt s;
1004
1005 if (CLI_HELP_REQUESTED)
1006 return cli_arg_help(cli, argc > 1,
1007 "USER", "Username of session to drop", NULL);
1008
1009 if (!config->cluster_iam_master)
1010 {
1011 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1012 return CLI_OK;
1013 }
1014
1015 if (!argc)
1016 {
1017 cli_print(cli, "Specify a user to drop");
1018 return CLI_OK;
1019 }
1020
1021 for (i = 0; i < argc; i++)
1022 {
1023 if (!(s = sessionbyuser(argv[i])))
1024 {
1025 cli_print(cli, "User %s is not connected", argv[i]);
1026 continue;
1027 }
1028
1029 if (session[s].ip && session[s].opened && !session[s].die)
1030 {
1031 int x;
1032
1033 cli_print(cli, "Dropping user %s", session[s].user);
1034 for (x = 0; x < MAXSESSION; x++)
1035 {
1036 if (!cli_session_kill[x])
1037 {
1038 cli_session_kill[x] = s;
1039 break;
1040 }
1041 }
1042 }
1043 }
1044
1045 return CLI_OK;
1046 }
1047
1048 int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1049 {
1050 int i;
1051 tunnelidt tid;
1052
1053 if (CLI_HELP_REQUESTED)
1054 return cli_arg_help(cli, argc > 1,
1055 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1056
1057 if (!config->cluster_iam_master)
1058 {
1059 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1060 return CLI_OK;
1061 }
1062
1063 if (!argc)
1064 {
1065 cli_print(cli, "Specify a tunnel to drop");
1066 return CLI_OK;
1067 }
1068
1069 for (i = 0; i < argc; i++)
1070 {
1071 int x;
1072
1073 if ((tid = atol(argv[i])) <= 0 || (tid >= MAXTUNNEL))
1074 {
1075 cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1076 continue;
1077 }
1078
1079 if (!tunnel[tid].ip)
1080 {
1081 cli_print(cli, "Tunnel %d is not connected", tid);
1082 continue;
1083 }
1084
1085 if (tunnel[tid].die)
1086 {
1087 cli_print(cli, "Tunnel %d is already being shut down", tid);
1088 continue;
1089 }
1090
1091 for (x = 0; x < MAXTUNNEL; x++)
1092 {
1093 if (!cli_tunnel_kill[x])
1094 {
1095 cli_tunnel_kill[x] = tid;
1096 cli_print(cli, "Tunnel %d shut down (%s)", tid, tunnel[tid].hostname);
1097 break;
1098 }
1099 }
1100 }
1101
1102 return CLI_OK;
1103 }
1104
1105 int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1106 {
1107 int i;
1108 sessionidt s;
1109
1110 if (CLI_HELP_REQUESTED)
1111 return cli_arg_help(cli, argc > 1,
1112 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1113
1114 if (!config->cluster_iam_master)
1115 {
1116 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1117 return CLI_OK;
1118 }
1119
1120 if (!argc)
1121 {
1122 cli_print(cli, "Specify a session id to drop");
1123 return CLI_OK;
1124 }
1125
1126 for (i = 0; i < argc; i++)
1127 {
1128 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1129 {
1130 cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1131 continue;
1132 }
1133
1134 if (session[s].opened && !session[s].die)
1135 {
1136 int x;
1137 for (x = 0; x < MAXSESSION; x++)
1138 {
1139 if (!cli_session_kill[x])
1140 {
1141 cli_session_kill[x] = s;
1142 break;
1143 }
1144 }
1145 cli_print(cli, "Dropping session %d", s);
1146 }
1147 else
1148 {
1149 cli_print(cli, "Session %d is not active.", s);
1150 }
1151 }
1152
1153 return CLI_OK;
1154 }
1155
1156 int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1157 {
1158 ipt ip;
1159 u16 port;
1160 sessionidt s;
1161
1162 if (CLI_HELP_REQUESTED)
1163 {
1164 switch (argc)
1165 {
1166 case 1:
1167 return cli_arg_help(cli, 0,
1168 "USER", "Username of session to snoop", NULL);
1169
1170 case 2:
1171 return cli_arg_help(cli, 0,
1172 "A.B.C.D", "IP address of snoop destination", NULL);
1173
1174 case 3:
1175 return cli_arg_help(cli, 0,
1176 "N", "Port of snoop destination", NULL);
1177
1178 case 4:
1179 if (!argv[3][1])
1180 return cli_arg_help(cli, 1, NULL);
1181
1182 default:
1183 return CLI_OK;
1184 }
1185 }
1186
1187 if (!config->cluster_iam_master)
1188 {
1189 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1190 return CLI_OK;
1191 }
1192
1193 if (argc < 3)
1194 {
1195 cli_print(cli, "Specify username ip port");
1196 return CLI_OK;
1197 }
1198
1199 if (!(s = sessionbyuser(argv[0])))
1200 {
1201 cli_print(cli, "User %s is not connected", argv[0]);
1202 return CLI_OK;
1203 }
1204
1205 ip = inet_addr(argv[1]);
1206 if (!ip || ip == INADDR_NONE)
1207 {
1208 cli_print(cli, "Cannot parse IP \"%s\"", argv[1]);
1209 return CLI_OK;
1210 }
1211
1212 port = atoi(argv[2]);
1213 if (!port)
1214 {
1215 cli_print(cli, "Invalid port %s", argv[2]);
1216 return CLI_OK;
1217 }
1218
1219 session[s].snoop_ip = ip;
1220 session[s].snoop_port = port;
1221
1222 cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(session[s].snoop_ip), session[s].snoop_port);
1223 return CLI_OK;
1224 }
1225
1226 int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1227 {
1228 int i;
1229 sessionidt s;
1230
1231 if (CLI_HELP_REQUESTED)
1232 return cli_arg_help(cli, argc > 1,
1233 "USER", "Username of session to un-snoop", NULL);
1234
1235 if (!config->cluster_iam_master)
1236 {
1237 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1238 return CLI_OK;
1239 }
1240
1241 if (!argc)
1242 {
1243 cli_print(cli, "Specify a user");
1244 return CLI_OK;
1245 }
1246
1247 for (i = 0; i < argc; i++)
1248 {
1249 if (!(s = sessionbyuser(argv[i])))
1250 {
1251 cli_print(cli, "User %s is not connected", argv[i]);
1252 continue;
1253 }
1254 session[s].snoop_ip = 0;
1255 session[s].snoop_port = 0;
1256
1257 cli_print(cli, "Not snooping user %s", argv[i]);
1258 }
1259 return CLI_OK;
1260 }
1261
1262 int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1263 {
1264 int i;
1265 sessionidt s;
1266
1267 if (CLI_HELP_REQUESTED)
1268 return cli_arg_help(cli, argc > 1,
1269 "USER", "Username of session to throttle", NULL);
1270
1271 if (!config->cluster_iam_master)
1272 {
1273 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1274 return CLI_OK;
1275 }
1276
1277 if (!argc)
1278 {
1279 cli_print(cli, "Specify a user");
1280 return CLI_OK;
1281 }
1282
1283 for (i = 0; i < argc; i++)
1284 {
1285 if (!(s = sessionbyuser(argv[i])))
1286 {
1287 cli_print(cli, "User %s is not connected", argv[i]);
1288 continue;
1289 }
1290 if (!throttle_session(s, config->rl_rate))
1291 cli_print(cli, "Error throttling %s", argv[i]);
1292 else
1293 cli_print(cli, "Throttling user %s", argv[i]);
1294 }
1295
1296 return CLI_OK;
1297 }
1298
1299 int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1300 {
1301 int i;
1302 sessionidt s;
1303
1304 if (CLI_HELP_REQUESTED)
1305 return cli_arg_help(cli, argc > 1,
1306 "USER", "Username of session to un-throttle", NULL);
1307
1308 if (!config->cluster_iam_master)
1309 {
1310 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1311 return CLI_OK;
1312 }
1313
1314 if (!argc)
1315 {
1316 cli_print(cli, "Specify a user");
1317 return CLI_OK;
1318 }
1319
1320 for (i = 0; i < argc; i++)
1321 {
1322 if (!(s = sessionbyuser(argv[i])))
1323 {
1324 cli_print(cli, "User %s is not connected", argv[i]);
1325 continue;
1326 }
1327 throttle_session(s, 0);
1328
1329 cli_print(cli, "Unthrottling user %s", argv[i]);
1330 }
1331
1332 return CLI_OK;
1333 }
1334
1335 int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1336 {
1337 int i;
1338
1339 if (CLI_HELP_REQUESTED)
1340 return cli_arg_help(cli, 1,
1341 "all", "Enable debugging for all except \"data\"",
1342 "critical", "", // FIXME: add descriptions
1343 "error", "",
1344 "warning", "",
1345 "info", "",
1346 "calls", "",
1347 "data", "",
1348 NULL);
1349
1350 if (!argc)
1351 {
1352 char *p = (char *) &debug_flags;
1353 for (i = 0; i < sizeof(debug_flags); i++)
1354 {
1355 if (p[i])
1356 {
1357 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1358 (debug_flags.critical) ? " critical" : "",
1359 (debug_flags.error) ? " error" : "",
1360 (debug_flags.warning) ? " warning" : "",
1361 (debug_flags.info) ? " info" : "",
1362 (debug_flags.calls) ? " calls" : "",
1363 (debug_flags.data) ? " data" : "");
1364
1365 return CLI_OK;
1366 }
1367 }
1368
1369 cli_print(cli, "Debugging off");
1370 return CLI_OK;
1371 }
1372
1373 for (i = 0; i < argc; i++)
1374 {
1375 int len = strlen(argv[i]);
1376
1377 if (argv[i][0] == 'c' && len < 2)
1378 len = 2; /* distinguish [cr]itical from [ca]lls */
1379
1380 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; }
1381 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; }
1382 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; }
1383 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; }
1384 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; }
1385 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; }
1386 if (!strncasecmp(argv[i], "all", len))
1387 {
1388 memset(&debug_flags, 1, sizeof(debug_flags));
1389 debug_flags.data = 0;
1390 continue;
1391 }
1392
1393 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1394 }
1395
1396 return CLI_OK;
1397 }
1398
1399 int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1400 {
1401 int i;
1402
1403 if (CLI_HELP_REQUESTED)
1404 return cli_arg_help(cli, 1,
1405 "all", "Disable all debugging",
1406 "critical", "", // FIXME: add descriptions
1407 "error", "",
1408 "warning", "",
1409 "info", "",
1410 "calls", "",
1411 "data", "",
1412 NULL);
1413
1414 if (!argc)
1415 {
1416 memset(&debug_flags, 0, sizeof(debug_flags));
1417 return CLI_OK;
1418 }
1419
1420 for (i = 0; i < argc; i++)
1421 {
1422 int len = strlen(argv[i]);
1423
1424 if (argv[i][0] == 'c' && len < 2)
1425 len = 2; /* distinguish [cr]itical from [ca]lls */
1426
1427 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; }
1428 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; }
1429 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; }
1430 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; }
1431 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; }
1432 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; }
1433 if (!strncasecmp(argv[i], "all", len))
1434 {
1435 memset(&debug_flags, 0, sizeof(debug_flags));
1436 continue;
1437 }
1438
1439 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1440 }
1441
1442 return CLI_OK;
1443 }
1444
1445 int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1446 {
1447 int i, firstfree = 0;
1448
1449 if (CLI_HELP_REQUESTED)
1450 return cli_arg_help(cli, argc > 1,
1451 "PLUGIN", "Name of plugin to load", NULL);
1452
1453 if (argc != 1)
1454 {
1455 cli_print(cli, "Specify a plugin to load");
1456 return CLI_OK;
1457 }
1458
1459 for (i = 0; i < MAXPLUGINS; i++)
1460 {
1461 if (!*config->plugins[i] && !firstfree)
1462 firstfree = i;
1463 if (strcmp(config->plugins[i], argv[0]) == 0)
1464 {
1465 cli_print(cli, "Plugin is already loaded");
1466 return CLI_OK;
1467 }
1468 }
1469
1470 if (firstfree)
1471 {
1472 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1473 config->reload_config = 1;
1474 cli_print(cli, "Loading plugin %s", argv[0]);
1475 }
1476
1477 return CLI_OK;
1478 }
1479
1480 int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1481 {
1482 int i;
1483
1484 if (CLI_HELP_REQUESTED)
1485 return cli_arg_help(cli, argc > 1,
1486 "PLUGIN", "Name of plugin to unload", NULL);
1487
1488 if (argc != 1)
1489 {
1490 cli_print(cli, "Specify a plugin to remove");
1491 return CLI_OK;
1492 }
1493
1494 for (i = 0; i < MAXPLUGINS; i++)
1495 {
1496 if (strcmp(config->plugins[i], argv[0]) == 0)
1497 {
1498 config->reload_config = 1;
1499 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1500 return CLI_OK;
1501 }
1502 }
1503
1504 cli_print(cli, "Plugin is not loaded");
1505 return CLI_OK;
1506 }
1507
1508 char *duration(time_t secs)
1509 {
1510 static char *buf = NULL;
1511 int p = 0;
1512
1513 if (!buf) buf = calloc(64, 1);
1514
1515 if (secs >= 86400)
1516 {
1517 int days = secs / 86400;
1518 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1519 secs %= 86400;
1520 }
1521
1522 if (secs >= 3600)
1523 {
1524 int mins = secs / 60;
1525 int hrs = mins / 60;
1526
1527 mins %= 60;
1528 sprintf(buf + p, "%d:%02d", hrs, mins);
1529 }
1530 else if (secs >= 60)
1531 {
1532 int mins = secs / 60;
1533 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1534 }
1535 else
1536 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1537
1538 return buf;
1539 }
1540
1541 int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1542 {
1543 FILE *fh;
1544 char buf[100], *p = buf, *loads[3];
1545 int i, num_sessions = 0;
1546
1547 if (CLI_HELP_REQUESTED)
1548 return CLI_HELP_NO_ARGS;
1549
1550 fh = fopen("/proc/loadavg", "r");
1551 fgets(buf, 100, fh);
1552 fclose(fh);
1553
1554 for (i = 0; i < 3; i++)
1555 loads[i] = strdup(strsep(&p, " "));
1556
1557 time(&time_now);
1558 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1559
1560 for (i = 1; i < MAXSESSION; i++)
1561 if (session[i].opened) num_sessions++;
1562
1563 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1564 buf,
1565 duration(time_now - config->start_time),
1566 num_sessions,
1567 loads[0], loads[1], loads[2]
1568 );
1569 for (i = 0; i < 3; i++)
1570 if (loads[i]) free(loads[i]);
1571
1572 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1573
1574 return CLI_OK;
1575 }
1576
1577 int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1578 {
1579 int i;
1580
1581 if (CLI_HELP_REQUESTED)
1582 {
1583 switch (argc)
1584 {
1585 case 1:
1586 {
1587 int len = strlen(argv[0])-1;
1588 for (i = 0; config_values[i].key; i++)
1589 if (!len || !strncmp(argv[0], config_values[i].key, len))
1590 cli_print(cli, " %s", config_values[i].key);
1591 }
1592
1593 return CLI_OK;
1594
1595 case 2:
1596 return cli_arg_help(cli, 0,
1597 "VALUE", "Value for variable", NULL);
1598
1599 case 3:
1600 if (!argv[2][1])
1601 return cli_arg_help(cli, 1, NULL);
1602
1603 default:
1604 return CLI_OK;
1605 }
1606 }
1607
1608 if (argc != 2)
1609 {
1610 cli_print(cli, "Specify variable and value");
1611 return CLI_OK;
1612 }
1613
1614 for (i = 0; config_values[i].key; i++)
1615 {
1616 void *value = ((void *)config) + config_values[i].offset;
1617 if (strcmp(config_values[i].key, argv[0]) == 0)
1618 {
1619 // Found a value to set
1620 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1621 switch (config_values[i].type)
1622 {
1623 case STRING:
1624 strncpy((char *)value, argv[1], config_values[i].size - 1);
1625 break;
1626 case INT:
1627 *(int *)value = atoi(argv[1]);
1628 break;
1629 case UNSIGNED_LONG:
1630 *(unsigned long *)value = atol(argv[1]);
1631 break;
1632 case SHORT:
1633 *(short *)value = atoi(argv[1]);
1634 break;
1635 case IP:
1636 *(unsigned *)value = inet_addr(argv[1]);
1637 break;
1638 case BOOL:
1639 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1640 *(int *)value = 1;
1641 else
1642 *(int *)value = 0;
1643 break;
1644 default:
1645 cli_print(cli, "Unknown variable type");
1646 break;
1647 }
1648 config->reload_config = 1;
1649 return CLI_OK;
1650 }
1651 }
1652
1653 cli_print(cli, "Unknown variable \"%s\"", argv[0]);
1654 return CLI_OK;
1655 }
1656
1657 int regular_stuff(struct cli_def *cli)
1658 {
1659 int i = debug_rb_tail;
1660 int reprompt = 0;
1661
1662 #ifdef RINGBUFFER
1663 while (i != ringbuffer->tail)
1664 {
1665 int show_message = 0;
1666
1667 if (*ringbuffer->buffer[i].message)
1668 {
1669 // Always show messages if we are doing general debug
1670 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
1671 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
1672 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
1673 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
1674 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
1675 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
1676 }
1677
1678 if (show_message)
1679 {
1680 ipt address = htonl(ringbuffer->buffer[i].address);
1681 char *ipaddr;
1682 struct in_addr addr;
1683
1684 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
1685 ipaddr = inet_ntoa(addr);
1686
1687 cli_print(cli, "\r%s-%s-%u-%u %s",
1688 debug_levels[(int)ringbuffer->buffer[i].level],
1689 ipaddr,
1690 ringbuffer->buffer[i].tunnel,
1691 ringbuffer->buffer[i].session,
1692 ringbuffer->buffer[i].message);
1693
1694 reprompt = 1;
1695 }
1696
1697 if (++i == ringbuffer->tail) break;
1698 if (i == RINGBUFFER_SIZE) i = 0;
1699 }
1700
1701 debug_rb_tail = ringbuffer->tail;
1702 if (reprompt)
1703 cli_reprompt(cli);
1704 #endif
1705 return CLI_OK;
1706 }