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