2150efc2149848f0382379514bb82b95751c0fff
[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.25 2004-11-11 03:07:42 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 <string.h>
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #include <arpa/inet.h>
18 #include <errno.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <dlfcn.h>
24 #include <libcli.h>
25 #include "l2tpns.h"
26 #include "util.h"
27 #include "cluster.h"
28 #include "tbf.h"
29 #include "ll.h"
30 #ifdef BGP
31 #include "bgp.h"
32 #include <netdb.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 struct configt *config;
43 extern struct config_descriptt config_values[];
44 #ifdef RINGBUFFER
45 extern struct Tringbuffer *ringbuffer;
46 #endif
47 extern struct cli_session_actions *cli_session_actions;
48 extern struct cli_tunnel_actions *cli_tunnel_actions;
49 extern tbft *filter_list;
50
51 char *debug_levels[] = {
52 "CRIT",
53 "ERROR",
54 "WARN",
55 "INFO",
56 "CALL",
57 "DATA",
58 };
59
60 struct
61 {
62 char critical;
63 char error;
64 char warning;
65 char info;
66 char calls;
67 char data;
68 } debug_flags;
69
70 int debug_session;
71 int debug_tunnel;
72 int debug_rb_tail;
73 FILE *save_config_fh;
74
75 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
76 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
77 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
78 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
79 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
80 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
81 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
82 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
83 static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
84 static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
85 static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc);
86 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
87 static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
88 static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
89 static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
90 static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
91 static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
92 static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
93 static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
94 static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
95 static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
96 static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
97 static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
98 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
99 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
100 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
101 static int regular_stuff(struct cli_def *cli);
102 static void parsemac(char *string, char mac[6]);
103
104 #ifdef BGP
105 #define MODE_CONFIG_BGP 8
106 static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc);
107 static int cmd_router_bgp_exit(struct cli_def *cli, char *command, char **argv, int argc);
108 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
109 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
110 static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc);
111 static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
112 static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
113 static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc);
114 #endif /* BGP */
115
116 void init_cli(char *hostname)
117 {
118 FILE *f;
119 char buf[4096];
120 struct cli_command *c;
121 struct cli_command *c2;
122 int on = 1;
123 struct sockaddr_in addr;
124
125 cli = cli_init();
126 if (hostname && *hostname)
127 cli_set_hostname(cli, hostname);
128 else
129 cli_set_hostname(cli, "l2tpns");
130
131 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
132 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
133 #ifdef BGP
134 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
135 #endif /* BGP */
136 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
137 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
138 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
139 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
140 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
141 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the currently running configuration");
142 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
143 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
144 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
145 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
146 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");
147 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
148
149 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
150 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
151 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
152
153 #ifdef STATISTICS
154 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
155
156 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
157 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
158 #endif
159
160 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
161
162 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
163 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
164 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
165
166 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable interception for a user");
167 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable throttling for a user");
168 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
169
170 #ifdef BGP
171 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
172 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP neighbour");
173 #endif /* BGP */
174
175 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
176 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable interception for a user");
177 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable throttling for a user");
178 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
179
180 #ifdef BGP
181 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
182 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP neighbour");
183
184 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
185 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
186
187 c = cli_register_command(cli, NULL, "router", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
188 cli_register_command(cli, c, "bgp", cmd_router_bgp, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Configure BGP");
189
190 cli_register_command(cli, NULL, "exit", cmd_router_bgp_exit, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Exit from BGP configuration");
191 cli_register_command(cli, NULL, "neighbour", cmd_router_bgp_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Configure BGP neighbour");
192
193 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, NULL);
194 cli_register_command(cli, c, "neighbour", cmd_router_bgp_no_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Remove BGP neighbour");
195 #endif /* BGP */
196
197 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
198 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
199 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
200 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
201
202 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
203 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
204
205 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
206 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
207
208 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
209
210 // Enable regular processing
211 cli_regular(cli, regular_stuff);
212
213 if (!(f = fopen(CLIUSERS, "r")))
214 {
215 LOG(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
216 }
217 else
218 {
219 while (fgets(buf, 4096, f))
220 {
221 char *p;
222 if (*buf == '#') continue;
223 if ((p = strchr(buf, '\r'))) *p = 0;
224 if ((p = strchr(buf, '\n'))) *p = 0;
225 if (!*buf) continue;
226 if (!(p = strchr((char *)buf, ':'))) continue;
227 *p++ = 0;
228 if (!strcmp(buf, "enable"))
229 {
230 cli_allow_enable(cli, p);
231 LOG(3, 0, 0, 0, "Setting enable password\n");
232 }
233 else
234 {
235 cli_allow_user(cli, buf, p);
236 LOG(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
237 }
238 }
239 fclose(f);
240 }
241
242 memset(&addr, 0, sizeof(addr));
243 clifd = socket(PF_INET, SOCK_STREAM, 6);
244 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
245 {
246 int flags;
247 // Set cli fd as non-blocking
248 flags = fcntl(clifd, F_GETFL, 0);
249 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
250 }
251 addr.sin_family = AF_INET;
252 addr.sin_port = htons(23);
253 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
254 {
255 LOG(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
256 return;
257 }
258 listen(clifd, 10);
259 }
260
261 void cli_do(int sockfd)
262 {
263 int require_auth = 1;
264 struct sockaddr_in addr;
265 int l = sizeof(addr);
266
267 if (fork_and_close()) return;
268 if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0)
269 {
270 LOG(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr.sin_addr.s_addr));
271 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
272 }
273 else
274 LOG(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
275
276 if (require_auth)
277 {
278 LOG(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
279 if (!cli->users) /* paranoia */
280 {
281 LOG(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
282 exit(0);
283 }
284 }
285 else
286 {
287 /* no username/pass required */
288 cli->users = 0;
289 }
290
291 debug_session = 0;
292 debug_tunnel = 0;
293 #ifdef RINGBUFFER
294 debug_rb_tail = ringbuffer->tail;
295 #endif
296 memset(&debug_flags, 0, sizeof(debug_flags));
297 debug_flags.critical = 1;
298
299 cli_loop(cli, sockfd);
300
301 close(sockfd);
302 LOG(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr.sin_addr.s_addr));
303 exit(0);
304 }
305
306 void cli_print_log(struct cli_def *cli, char *string)
307 {
308 LOG(3, 0, 0, 0, "%s\n", string);
309 }
310
311 void cli_do_file(FILE *fh)
312 {
313 LOG(3, 0, 0, 0, "Reading configuration file\n");
314 cli_print_callback(cli, cli_print_log);
315 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
316 cli_print_callback(cli, NULL);
317 }
318
319 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
320 {
321 va_list ap;
322 char *desc;
323 char buf[16];
324 char *p;
325
326 va_start(ap, entry);
327 while (entry)
328 {
329 /* allow one %d */
330 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
331 {
332 int v = va_arg(ap, int);
333 snprintf(buf, sizeof(buf), entry, v);
334 p = buf;
335 }
336 else
337 p = entry;
338
339 desc = va_arg(ap, char *);
340 if (desc && *desc)
341 cli_print(cli, " %-20s %s", p, desc);
342 else
343 cli_print(cli, " %s", p);
344
345 entry = desc ? va_arg(ap, char *) : 0;
346 }
347
348 va_end(ap);
349 if (cr_ok)
350 cli_print(cli, " <cr>");
351
352 return CLI_OK;
353 }
354
355 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
356 {
357 int i;
358
359 if (CLI_HELP_REQUESTED)
360 return cli_arg_help(cli, 1,
361 "<1-%d>", MAXSESSION-1, "Show specific session by id",
362 NULL);
363
364 time(&time_now);
365 if (argc > 0)
366 {
367 // Show individual session
368 for (i = 0; i < argc; i++)
369 {
370 unsigned int s, b_in, b_out;
371 s = atoi(argv[i]);
372 if (s <= 0 || s >= MAXSESSION)
373 {
374 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
375 continue;
376 }
377 cli_print(cli, "\r\nSession %d:", s);
378 cli_print(cli, "\tUser:\t\t%s", session[s].user[0] ? session[s].user : "none");
379 cli_print(cli, "\tCalling Num:\t%s", session[s].calling);
380 cli_print(cli, "\tCalled Num:\t%s", session[s].called);
381 cli_print(cli, "\tTunnel ID:\t%d", session[s].tunnel);
382 cli_print(cli, "\tIP address:\t%s", inet_toa(htonl(session[s].ip)));
383 cli_print(cli, "\tUnique SID:\t%lu", session[s].unique_id);
384 cli_print(cli, "\tIdle time:\t%u seconds", abs(time_now - session[s].last_packet));
385 cli_print(cli, "\tNext Recv:\t%u", session[s].nr);
386 cli_print(cli, "\tNext Send:\t%u", session[s].ns);
387 cli_print(cli, "\tBytes In/Out:\t%lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin);
388 cli_print(cli, "\tPkts In/Out:\t%lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin);
389 cli_print(cli, "\tMRU:\t\t%d", session[s].mru);
390 cli_print(cli, "\tRadius Session:\t%u", session[s].radius);
391 cli_print(cli, "\tRx Speed:\t%lu", session[s].rx_connect_speed);
392 cli_print(cli, "\tTx Speed:\t%lu", session[s].tx_connect_speed);
393 if (session[s].snoop_ip && session[s].snoop_port)
394 cli_print(cli, "\tIntercepted:\t%s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port);
395 else
396 cli_print(cli, "\tIntercepted:\tno");
397
398 cli_print(cli, "\tWalled Garden:\t%s", session[s].walled_garden ? "YES" : "no");
399 {
400 int t = (session[s].throttle_in || session[s].throttle_out);
401 cli_print(cli, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
402 t ? "YES" : "no", t ? " (" : "",
403 session[s].throttle_in, session[s].throttle_in ? "kbps" : t ? "-" : "",
404 t ? "/" : "",
405 session[s].throttle_out, session[s].throttle_out ? "kbps" : t ? "-" : "",
406 t ? ")" : "");
407 }
408
409 b_in = session[s].tbf_in;
410 b_out = session[s].tbf_out;
411 if (b_in || b_out)
412 cli_print(cli, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
413 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
414 "ByteSent", "PackSent", "PackDrop", "PackDelay");
415
416 if (b_in)
417 cli_print(cli, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
418 b_in,
419 (filter_list[b_in].next ? "*" : " "),
420 (b_in < 100 ? "\t" : ""),
421 filter_list[b_in].rate * 8,
422 filter_list[b_in].credit,
423 filter_list[b_in].queued,
424 filter_list[b_in].b_queued,
425 filter_list[b_in].p_queued,
426 filter_list[b_in].b_sent,
427 filter_list[b_in].p_sent,
428 filter_list[b_in].p_dropped,
429 filter_list[b_in].p_delayed);
430
431 if (b_out)
432 cli_print(cli, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
433 b_out,
434 (filter_list[b_out].next ? "*" : " "),
435 (b_out < 100 ? "\t" : ""),
436 filter_list[b_out].rate * 8,
437 filter_list[b_out].credit,
438 filter_list[b_out].queued,
439 filter_list[b_out].b_queued,
440 filter_list[b_out].p_queued,
441 filter_list[b_out].b_sent,
442 filter_list[b_out].p_sent,
443 filter_list[b_out].p_dropped,
444 filter_list[b_out].p_delayed);
445
446 }
447 return CLI_OK;
448 }
449
450 // Show Summary
451 cli_print(cli, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
452 "SID",
453 "TID",
454 "Username",
455 "IP",
456 "I",
457 "T",
458 "G",
459 "opened",
460 "downloaded",
461 "uploaded",
462 "idle",
463 "LAC",
464 "CLI");
465
466 for (i = 1; i < MAXSESSION; i++)
467 {
468 char *userip, *tunnelip;
469 if (!session[i].opened) continue;
470 userip = strdup(inet_toa(htonl(session[i].ip)));
471 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
472 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
473 i,
474 session[i].tunnel,
475 session[i].user[0] ? session[i].user : "*",
476 userip,
477 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
478 (session[i].throttle_in || session[i].throttle_out) ? "Y" : "N",
479 (session[i].walled_garden) ? "Y" : "N",
480 abs(time_now - (unsigned long)session[i].opened),
481 (unsigned long)session[i].total_cout,
482 (unsigned long)session[i].total_cin,
483 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
484 tunnelip,
485 session[i].calling[0] ? session[i].calling : "*");
486 if (userip) free(userip);
487 if (tunnelip) free(tunnelip);
488 }
489 return CLI_OK;
490 }
491
492 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
493 {
494 int i, x, show_all = 0;
495 char *states[] = {
496 "Free",
497 "Open",
498 "Closing",
499 "Opening",
500 };
501
502 if (CLI_HELP_REQUESTED)
503 {
504 if (argc > 1)
505 return cli_arg_help(cli, 1,
506 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
507 NULL);
508
509 return cli_arg_help(cli, 1,
510 "all", "Show all tunnels, including unused",
511 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
512 NULL);
513 }
514
515 time(&time_now);
516 if (argc > 0)
517 {
518 if (strcmp(argv[0], "all") == 0)
519 {
520 show_all = 1;
521 }
522 else
523 {
524 // Show individual tunnel
525 for (i = 0; i < argc; i++)
526 {
527 char s[65535] = {0};
528 unsigned int t;
529 t = atoi(argv[i]);
530 if (t <= 0 || t >= MAXTUNNEL)
531 {
532 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
533 continue;
534 }
535 cli_print(cli, "\r\nTunnel %d:", t);
536 cli_print(cli, "\tState:\t\t%s", states[tunnel[t].state]);
537 cli_print(cli, "\tHostname:\t%s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
538 cli_print(cli, "\tRemote IP:\t%s", inet_toa(htonl(tunnel[t].ip)));
539 cli_print(cli, "\tRemote Port:\t%d", tunnel[t].port);
540 cli_print(cli, "\tRx Window:\t%u", tunnel[t].window);
541 cli_print(cli, "\tNext Recv:\t%u", tunnel[t].nr);
542 cli_print(cli, "\tNext Send:\t%u", tunnel[t].ns);
543 cli_print(cli, "\tQueue Len:\t%u", tunnel[t].controlc);
544 cli_print(cli, "\tLast Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
545
546 for (x = 0; x < MAXSESSION; x++)
547 if (session[x].tunnel == t && session[x].opened && !session[x].die)
548 sprintf(s, "%s%u ", s, x);
549
550 cli_print(cli, "\tSessions:\t%s", s);
551 }
552 return CLI_OK;
553 }
554 }
555
556 // Show tunnel summary
557 cli_print(cli, "%4s %20s %20s %6s %s",
558 "TID",
559 "Hostname",
560 "IP",
561 "State",
562 "Sessions");
563
564 for (i = 1; i < MAXTUNNEL; i++)
565 {
566 int sessions = 0;
567 if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
568
569 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
570 cli_print(cli, "%4d %20s %20s %6s %6d",
571 i,
572 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
573 inet_toa(htonl(tunnel[i].ip)),
574 states[tunnel[i].state],
575 sessions);
576 }
577
578 return CLI_OK;
579 }
580
581 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
582 {
583 char sid[32][8];
584 char *sargv[32];
585 int sargc = 0;
586 int i;
587
588 if (CLI_HELP_REQUESTED)
589 return cli_arg_help(cli, 1,
590 "USER", "Show details for specific username",
591 NULL);
592
593 for (i = 0; i < MAXSESSION; i++)
594 {
595 if (!session[i].opened) continue;
596 if (!session[i].user[0]) continue;
597 if (argc > 0)
598 {
599 int j;
600 for (j = 0; j < argc && sargc < 32; j++)
601 {
602 if (strcmp(argv[j], session[i].user) == 0)
603 {
604 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
605 sargv[sargc] = sid[sargc];
606 sargc++;
607 }
608 }
609
610 continue;
611 }
612
613 cli_print(cli, "%s", session[i].user);
614 }
615
616 if (sargc > 0)
617 return cmd_show_session(cli, "users", sargv, sargc);
618
619 return CLI_OK;
620 }
621
622 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
623 {
624 if (CLI_HELP_REQUESTED)
625 return CLI_HELP_NO_ARGS;
626
627 cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
628 cli_print(cli, "%-10s %8lu %8lu %8lu", "RX",
629 GET_STAT(tun_rx_bytes),
630 GET_STAT(tun_rx_packets),
631 GET_STAT(tun_rx_errors));
632 cli_print(cli, "%-10s %8lu %8lu %8lu", "TX",
633 GET_STAT(tun_tx_bytes),
634 GET_STAT(tun_tx_packets),
635 GET_STAT(tun_tx_errors));
636 cli_print(cli, "");
637
638 cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
639 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX",
640 GET_STAT(tunnel_rx_bytes),
641 GET_STAT(tunnel_rx_packets),
642 GET_STAT(tunnel_rx_errors),
643 0L);
644 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX",
645 GET_STAT(tunnel_tx_bytes),
646 GET_STAT(tunnel_tx_packets),
647 GET_STAT(tunnel_tx_errors),
648 GET_STAT(tunnel_retries));
649 cli_print(cli, "");
650
651 cli_print(cli, "%-30s%-10s", "Counter", "Value");
652 cli_print(cli, "-----------------------------------------");
653 cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries));
654 cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent));
655 cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped));
656 cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created));
657 cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created));
658 cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout));
659 cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout));
660 cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout));
661 cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow));
662 cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow));
663 cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow));
664 cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated));
665 cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed));
666 cli_print(cli, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded));
667 cli_print(cli, "%-30s%lu", "recv_forward", GET_STAT(recv_forward));
668
669
670 #ifdef STATISTICS
671 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
672 cli_print(cli, "-----------------------------------------");
673 cli_print(cli, "%-30s%lu", "call_processtun", GET_STAT(call_processtun));
674 cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout));
675 cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp));
676 cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap));
677 cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap));
678 cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp));
679 cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp));
680 cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin));
681 cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp));
682 cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad));
683 cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp));
684 cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp));
685 cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap));
686 cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip));
687 cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
688 cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend));
689 cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill));
690 cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
691 cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill));
692 cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
693 cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup));
694 cli_print(cli, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address));
695 cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address));
696 cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
697 cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend));
698 cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry));
699 #endif
700 return CLI_OK;
701 }
702
703 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
704 {
705 int tag = 0;
706 int file = 0;
707 int i = 0;
708
709 if (CLI_HELP_REQUESTED)
710 return cli_arg_help(cli, 1,
711 "tag", "Include CVS release tag",
712 "file", "Include file versions",
713 NULL);
714
715 for (i = 0; i < argc; i++)
716 if (!strcmp(argv[i], "tag"))
717 tag++;
718 else if (!strcmp(argv[i], "file"))
719 file++;
720
721 cli_print(cli, "L2TPNS %s", VERSION);
722 if (tag)
723 {
724 char const *p = strchr(cvs_name, ':');
725 char const *e;
726 if (p)
727 {
728 p++;
729 while (isspace(*p))
730 p++;
731 }
732
733 if (!p || *p == '$')
734 p = "HEAD";
735
736 e = strpbrk(p, " \t$");
737 cli_print(cli, "Tag: %.*s", e ? e - p + 1 : strlen(p), p);
738 }
739
740 if (file)
741 {
742 extern linked_list *loaded_plugins;
743 void *p;
744
745 cli_print(cli, "Files:");
746 cli_print(cli, " %s", cvs_id_arp);
747 #ifdef BGP
748 cli_print(cli, " %s", cvs_id_bgp);
749 #endif /* BGP */
750 cli_print(cli, " %s", cvs_id_cli);
751 cli_print(cli, " %s", cvs_id_cluster);
752 cli_print(cli, " %s", cvs_id_constants);
753 cli_print(cli, " %s", cvs_id_control);
754 cli_print(cli, " %s", cvs_id_icmp);
755 cli_print(cli, " %s", cvs_id_l2tpns);
756 cli_print(cli, " %s", cvs_id_ll);
757 cli_print(cli, " %s", cvs_id_md5);
758 cli_print(cli, " %s", cvs_id_ppp);
759 cli_print(cli, " %s", cvs_id_radius);
760 cli_print(cli, " %s", cvs_id_tbf);
761 cli_print(cli, " %s", cvs_id_util);
762
763 ll_reset(loaded_plugins);
764 while ((p = ll_next(loaded_plugins)))
765 {
766 char const **id = dlsym(p, "cvs_id");
767 if (id)
768 cli_print(cli, " %s", *id);
769 }
770 }
771
772 return CLI_OK;
773 }
774
775 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
776 {
777 int i;
778 int used = 0, free = 0, show_all = 0;
779
780 if (!config->cluster_iam_master)
781 {
782 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
783 return CLI_OK;
784 }
785
786 if (CLI_HELP_REQUESTED)
787 {
788 if (argc > 1)
789 return cli_arg_help(cli, 1, NULL);
790
791 return cli_arg_help(cli, 1,
792 "all", "Show all pool addresses, including unused",
793 NULL);
794 }
795
796 if (argc > 0 && strcmp(argv[0], "all") == 0)
797 show_all = 1;
798
799 time(&time_now);
800 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
801 for (i = 0; i < MAXIPPOOL; i++)
802 {
803 if (!ip_address_pool[i].address) continue;
804 if (ip_address_pool[i].assigned)
805 {
806 cli_print(cli, "%-15s\tY %8d %s",
807 inet_toa(htonl(ip_address_pool[i].address)), ip_address_pool[i].session, session[ip_address_pool[i].session].user);
808
809 used++;
810 }
811 else
812 {
813 if (ip_address_pool[i].last)
814 cli_print(cli, "%-15s\tN %8s [%s] %ds",
815 inet_toa(htonl(ip_address_pool[i].address)), "",
816 ip_address_pool[i].user, time_now - ip_address_pool[i].last);
817 else if (show_all)
818 cli_print(cli, "%-15s\tN", inet_toa(htonl(ip_address_pool[i].address)));
819
820 free++;
821 }
822 }
823
824 if (!show_all)
825 cli_print(cli, "(Not displaying unused addresses)");
826
827 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
828 return CLI_OK;
829 }
830
831 void print_save_config(struct cli_def *cli, char *string)
832 {
833 if (save_config_fh)
834 fprintf(save_config_fh, "%s\n", string);
835 }
836
837 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
838 {
839 if (CLI_HELP_REQUESTED)
840 return CLI_HELP_NO_ARGS;
841
842 if ((save_config_fh = fopen(config->config_file, "w")))
843 {
844 cli_print(cli, "Writing configuration");
845 cli_print_callback(cli, print_save_config);
846 cmd_show_run(cli, command, argv, argc);
847 cli_print_callback(cli, NULL);
848 fclose(save_config_fh);
849 }
850 else
851 {
852 cli_print(cli, "Error writing configuration: %s", strerror(errno));
853 }
854 return CLI_OK;
855 }
856
857 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
858 {
859 int i;
860
861 if (CLI_HELP_REQUESTED)
862 return CLI_HELP_NO_ARGS;
863
864 cli_print(cli, "# Current configuration:");
865
866 for (i = 0; config_values[i].key; i++)
867 {
868 void *value = ((void *)config) + config_values[i].offset;
869 if (config_values[i].type == STRING)
870 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value);
871 else if (config_values[i].type == IP)
872 cli_print(cli, "set %s %s", config_values[i].key, inet_toa(*(unsigned *)value));
873 else if (config_values[i].type == SHORT)
874 cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value);
875 else if (config_values[i].type == BOOL)
876 cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no");
877 else if (config_values[i].type == INT)
878 cli_print(cli, "set %s %d", config_values[i].key, *(int *)value);
879 else if (config_values[i].type == UNSIGNED_LONG)
880 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value);
881 else if (config_values[i].type == MAC)
882 cli_print(cli, "set %s %02x%02x.%02x%02x.%02x%02x", config_values[i].key,
883 *(unsigned short *)(value + 0),
884 *(unsigned short *)(value + 1),
885 *(unsigned short *)(value + 2),
886 *(unsigned short *)(value + 3),
887 *(unsigned short *)(value + 4),
888 *(unsigned short *)(value + 5));
889 }
890
891 cli_print(cli, "# Plugins");
892 for (i = 0; i < MAXPLUGINS; i++)
893 {
894 if (*config->plugins[i])
895 {
896 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
897 }
898 }
899
900 #ifdef BGP
901 if (config->as_number)
902 {
903 int k;
904 int h;
905
906 cli_print(cli, "# BGP");
907 cli_print(cli, "router bgp %u", config->as_number);
908 for (i = 0; i < BGP_NUM_PEERS; i++)
909 {
910 if (!config->neighbour[i].name[0])
911 continue;
912
913 cli_print(cli, " neighbour %s remote-as %u", config->neighbour[i].name, config->neighbour[i].as);
914
915 k = config->neighbour[i].keepalive;
916 h = config->neighbour[i].hold;
917
918 if (k == -1)
919 {
920 if (h == -1)
921 continue;
922
923 k = BGP_KEEPALIVE_TIME;
924 }
925
926 if (h == -1)
927 h = BGP_HOLD_TIME;
928
929 cli_print(cli, " neighbour %s timers %d %d", config->neighbour[i].name, k, h);
930 }
931 }
932 #endif
933
934 cli_print(cli, "# end");
935 return CLI_OK;
936 }
937
938 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
939 {
940 int i, free = 0, used = 0, show_all = 0;
941 char *states[] = {
942 "NULL",
943 "CHAP",
944 "AUTH",
945 "IPCP",
946 "START",
947 "STOP",
948 "WAIT",
949 };
950
951 if (CLI_HELP_REQUESTED)
952 {
953 if (argc > 1)
954 return cli_arg_help(cli, 1, NULL);
955
956 return cli_arg_help(cli, 1,
957 "all", "Show all RADIUS sessions, including unused",
958 NULL);
959 }
960
961 cli_print(cli, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
962
963 time(&time_now);
964
965 if (argc > 0 && strcmp(argv[0], "all") == 0)
966 show_all = 1;
967
968 for (i = 1; i < MAXRADIUS; i++)
969 {
970 if (radius[i].state == RADIUSNULL)
971 free++;
972 else
973 used++;
974
975 if (!show_all && radius[i].state == RADIUSNULL) continue;
976
977 cli_print(cli, "%6d%6d%5d%6s%9d%9u%4d",
978 i,
979 i >> RADIUS_SHIFT,
980 i & RADIUS_MASK,
981 states[radius[i].state],
982 radius[i].session,
983 radius[i].retry,
984 radius[i].try);
985 }
986
987 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
988
989 return CLI_OK;
990 }
991
992 static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
993 {
994 int i;
995
996 if (CLI_HELP_REQUESTED)
997 return CLI_HELP_NO_ARGS;
998
999 cli_print(cli, "Plugins currently loaded:");
1000 for (i = 0; i < MAXPLUGINS; i++)
1001 if (*config->plugins[i])
1002 cli_print(cli, " %s", config->plugins[i]);
1003
1004 return CLI_OK;
1005 }
1006
1007 static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1008 {
1009 int i;
1010
1011 if (CLI_HELP_REQUESTED)
1012 return CLI_HELP_NO_ARGS;
1013
1014 cli_print(cli, "%5s %4s %-32s %7s %6s %6s %6s",
1015 "SID",
1016 "TID",
1017 "Username",
1018 "Rate In",
1019 "Out",
1020 "TBFI",
1021 "TBFO");
1022
1023 for (i = 0; i < MAXSESSION; i++)
1024 {
1025 if (session[i].throttle_in || session[i].throttle_out)
1026 cli_print(cli, "%5d %4d %-32s %6d %6d %6d %6d",
1027 i,
1028 session[i].tunnel,
1029 session[i].user,
1030 session[i].throttle_in,
1031 session[i].throttle_out,
1032 session[i].tbf_in,
1033 session[i].tbf_out);
1034 }
1035
1036 return CLI_OK;
1037 }
1038
1039 static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
1040 {
1041 if (CLI_HELP_REQUESTED)
1042 return CLI_HELP_NO_ARGS;
1043
1044 cli_print(cli, " _\n"
1045 "//\\\n"
1046 "V \\\n"
1047 " \\ \\_\n"
1048 " \\,'.`-.\n"
1049 " |\\ `. `.\n"
1050 " ( \\ `. `-. _,.-:\\\n"
1051 " \\ \\ `. `-._ __..--' ,-';/\n"
1052 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1053 " `. `. `-._ __..--' ,' /\n"
1054 " `. `-_ ``--..'' _.-' ,'\n"
1055 " `-_ `-.___ __,--' ,'\n"
1056 " `-.__ `----\"\"\" __.-'\n"
1057 "hh `--..____..--'");
1058
1059 return CLI_OK;
1060 }
1061
1062 static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
1063 {
1064 if (CLI_HELP_REQUESTED)
1065 return CLI_HELP_NO_ARGS;
1066
1067 cli_print(cli, "Counters cleared");
1068 SET_STAT(last_reset, time(NULL));
1069 return CLI_OK;
1070 }
1071
1072 static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
1073 {
1074 int i;
1075 sessionidt s;
1076
1077 if (CLI_HELP_REQUESTED)
1078 return cli_arg_help(cli, argc > 1,
1079 "USER", "Username of session to drop", NULL);
1080
1081 if (!config->cluster_iam_master)
1082 {
1083 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1084 return CLI_OK;
1085 }
1086
1087 if (!argc)
1088 {
1089 cli_print(cli, "Specify a user to drop");
1090 return CLI_OK;
1091 }
1092
1093 for (i = 0; i < argc; i++)
1094 {
1095 if (!(s = sessionbyuser(argv[i])))
1096 {
1097 cli_print(cli, "User %s is not connected", argv[i]);
1098 continue;
1099 }
1100
1101 if (session[s].ip && session[s].opened && !session[s].die)
1102 {
1103 cli_print(cli, "Dropping user %s", session[s].user);
1104 cli_session_actions[s].action |= CLI_SESS_KILL;
1105 }
1106 }
1107
1108 return CLI_OK;
1109 }
1110
1111 static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1112 {
1113 int i;
1114 tunnelidt t;
1115
1116 if (CLI_HELP_REQUESTED)
1117 return cli_arg_help(cli, argc > 1,
1118 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1119
1120 if (!config->cluster_iam_master)
1121 {
1122 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1123 return CLI_OK;
1124 }
1125
1126 if (!argc)
1127 {
1128 cli_print(cli, "Specify a tunnel to drop");
1129 return CLI_OK;
1130 }
1131
1132 for (i = 0; i < argc; i++)
1133 {
1134 if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL))
1135 {
1136 cli_print(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1137 continue;
1138 }
1139
1140 if (!tunnel[t].ip)
1141 {
1142 cli_print(cli, "Tunnel %d is not connected", t);
1143 continue;
1144 }
1145
1146 if (tunnel[t].die)
1147 {
1148 cli_print(cli, "Tunnel %d is already being shut down", t);
1149 continue;
1150 }
1151
1152 cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname);
1153 cli_tunnel_actions[t].action |= CLI_TUN_KILL;
1154 }
1155
1156 return CLI_OK;
1157 }
1158
1159 static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1160 {
1161 int i;
1162 sessionidt s;
1163
1164 if (CLI_HELP_REQUESTED)
1165 return cli_arg_help(cli, argc > 1,
1166 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1167
1168 if (!config->cluster_iam_master)
1169 {
1170 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1171 return CLI_OK;
1172 }
1173
1174 if (!argc)
1175 {
1176 cli_print(cli, "Specify a session id to drop");
1177 return CLI_OK;
1178 }
1179
1180 for (i = 0; i < argc; i++)
1181 {
1182 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1183 {
1184 cli_print(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1185 continue;
1186 }
1187
1188 if (session[s].ip && session[s].opened && !session[s].die)
1189 {
1190 cli_print(cli, "Dropping session %d", s);
1191 cli_session_actions[s].action |= CLI_SESS_KILL;
1192 }
1193 else
1194 {
1195 cli_print(cli, "Session %d is not active.", s);
1196 }
1197 }
1198
1199 return CLI_OK;
1200 }
1201
1202 static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1203 {
1204 ipt ip;
1205 u16 port;
1206 sessionidt s;
1207
1208 if (CLI_HELP_REQUESTED)
1209 {
1210 switch (argc)
1211 {
1212 case 1:
1213 return cli_arg_help(cli, 0,
1214 "USER", "Username of session to snoop", NULL);
1215
1216 case 2:
1217 return cli_arg_help(cli, 0,
1218 "A.B.C.D", "IP address of snoop destination", NULL);
1219
1220 case 3:
1221 return cli_arg_help(cli, 0,
1222 "N", "Port of snoop destination", NULL);
1223
1224 case 4:
1225 if (!argv[3][1])
1226 return cli_arg_help(cli, 1, NULL);
1227
1228 default:
1229 return CLI_OK;
1230 }
1231 }
1232
1233 if (!config->cluster_iam_master)
1234 {
1235 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1236 return CLI_OK;
1237 }
1238
1239 if (argc < 3)
1240 {
1241 cli_print(cli, "Specify username, ip and port");
1242 return CLI_OK;
1243 }
1244
1245 if (!(s = sessionbyuser(argv[0])))
1246 {
1247 cli_print(cli, "User %s is not connected", argv[0]);
1248 return CLI_OK;
1249 }
1250
1251 ip = inet_addr(argv[1]);
1252 if (!ip || ip == INADDR_NONE)
1253 {
1254 cli_print(cli, "Cannot parse IP \"%s\"", argv[1]);
1255 return CLI_OK;
1256 }
1257
1258 port = atoi(argv[2]);
1259 if (!port)
1260 {
1261 cli_print(cli, "Invalid port %s", argv[2]);
1262 return CLI_OK;
1263 }
1264
1265 cli_print(cli, "Snooping user %s to %s:%d", argv[0], inet_toa(ip), port);
1266 cli_session_actions[s].snoop_ip = ip;
1267 cli_session_actions[s].snoop_port = port;
1268 cli_session_actions[s].action |= CLI_SESS_SNOOP;
1269
1270 return CLI_OK;
1271 }
1272
1273 static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1274 {
1275 int i;
1276 sessionidt s;
1277
1278 if (CLI_HELP_REQUESTED)
1279 return cli_arg_help(cli, argc > 1,
1280 "USER", "Username of session to unsnoop", NULL);
1281
1282 if (!config->cluster_iam_master)
1283 {
1284 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1285 return CLI_OK;
1286 }
1287
1288 if (!argc)
1289 {
1290 cli_print(cli, "Specify a user to unsnoop");
1291 return CLI_OK;
1292 }
1293
1294 for (i = 0; i < argc; i++)
1295 {
1296 if (!(s = sessionbyuser(argv[i])))
1297 {
1298 cli_print(cli, "User %s is not connected", argv[i]);
1299 continue;
1300 }
1301
1302 cli_print(cli, "Not snooping user %s", argv[i]);
1303 cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
1304 }
1305
1306 return CLI_OK;
1307 }
1308
1309 static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1310 {
1311 int rate_in = 0;
1312 int rate_out = 0;
1313 sessionidt s;
1314
1315 /*
1316 throttle USER - throttle in/out to default rate
1317 throttle USER RATE - throttle in/out to default rate
1318 throttle USER in RATE - throttle input only
1319 throttle USER out RATE - throttle output only
1320 throttle USER in RATE out RATE - throttle both
1321 */
1322
1323 if (CLI_HELP_REQUESTED)
1324 {
1325 switch (argc)
1326 {
1327 case 1:
1328 return cli_arg_help(cli, 0,
1329 "USER", "Username of session to throttle", NULL);
1330
1331 case 2:
1332 return cli_arg_help(cli, 1,
1333 "RATE", "Rate in kbps (in and out)",
1334 "in", "Select incoming rate",
1335 "out", "Select outgoing rate", NULL);
1336
1337 case 4:
1338 return cli_arg_help(cli, 1,
1339 "in", "Select incoming rate",
1340 "out", "Select outgoing rate", NULL);
1341
1342 case 3:
1343 if (isdigit(argv[1][0]))
1344 return cli_arg_help(cli, 1, NULL);
1345
1346 case 5:
1347 return cli_arg_help(cli, 0, "RATE", "Rate in kbps", NULL);
1348
1349 default:
1350 return cli_arg_help(cli, argc > 1, NULL);
1351 }
1352 }
1353
1354 if (!config->cluster_iam_master)
1355 {
1356 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1357 return CLI_OK;
1358 }
1359
1360 if (argc == 0)
1361 {
1362 cli_print(cli, "Specify a user to throttle");
1363 return CLI_OK;
1364 }
1365
1366 if (!(s = sessionbyuser(argv[0])))
1367 {
1368 cli_print(cli, "User %s is not connected", argv[0]);
1369 return CLI_OK;
1370 }
1371
1372 if (argc == 1)
1373 {
1374 rate_in = rate_out = config->rl_rate;
1375 }
1376 else if (argc == 2)
1377 {
1378 rate_in = rate_out = atoi(argv[1]);
1379 if (rate_in < 1)
1380 {
1381 cli_print(cli, "Invalid rate \"%s\"", argv[1]);
1382 return CLI_OK;
1383 }
1384 }
1385 else if (argc == 3 || argc == 5)
1386 {
1387 int i;
1388 for (i = 1; i < argc - 1; i += 2)
1389 {
1390 int len = strlen(argv[i]);
1391 int r = 0;
1392 if (!strncasecmp(argv[i], "in", len))
1393 r = rate_in = atoi(argv[i+1]);
1394 else if (!strncasecmp(argv[i], "out", len))
1395 r = rate_out = atoi(argv[i+1]);
1396
1397 if (r < 1)
1398 {
1399 cli_print(cli, "Invalid rate specification \"%s %s\"", argv[i], argv[i+1]);
1400 return CLI_OK;
1401 }
1402 }
1403 }
1404 else
1405 {
1406 cli_print(cli, "Invalid arguments");
1407 return CLI_OK;
1408 }
1409
1410 if ((rate_in && session[s].throttle_in) || (rate_out && session[s].throttle_out))
1411 {
1412 cli_print(cli, "User %s already throttled, unthrottle first", argv[0]);
1413 return CLI_OK;
1414 }
1415
1416 cli_session_actions[s].throttle_in = cli_session_actions[s].throttle_out = -1;
1417 if (rate_in && session[s].throttle_in != rate_in)
1418 cli_session_actions[s].throttle_in = rate_in;
1419
1420 if (rate_out && session[s].throttle_out != rate_out)
1421 cli_session_actions[s].throttle_out = rate_out;
1422
1423 if (cli_session_actions[s].throttle_in == -1 &&
1424 cli_session_actions[s].throttle_out == -1)
1425 {
1426 cli_print(cli, "User %s already throttled at this rate", argv[0]);
1427 return CLI_OK;
1428 }
1429
1430 cli_print(cli, "Throttling user %s", argv[0]);
1431 cli_session_actions[s].action |= CLI_SESS_THROTTLE;
1432
1433 return CLI_OK;
1434 }
1435
1436 static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1437 {
1438 int i;
1439 sessionidt s;
1440
1441 if (CLI_HELP_REQUESTED)
1442 return cli_arg_help(cli, argc > 1,
1443 "USER", "Username of session to unthrottle", NULL);
1444
1445 if (!config->cluster_iam_master)
1446 {
1447 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
1448 return CLI_OK;
1449 }
1450
1451 if (!argc)
1452 {
1453 cli_print(cli, "Specify a user to unthrottle");
1454 return CLI_OK;
1455 }
1456
1457 for (i = 0; i < argc; i++)
1458 {
1459 if (!(s = sessionbyuser(argv[i])))
1460 {
1461 cli_print(cli, "User %s is not connected", argv[i]);
1462 continue;
1463 }
1464
1465 if (session[s].throttle_in || session[s].throttle_out)
1466 {
1467 cli_print(cli, "Unthrottling user %s", argv[i]);
1468 cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
1469 }
1470 else
1471 {
1472 cli_print(cli, "User %s not throttled", argv[i]);
1473 }
1474 }
1475
1476 return CLI_OK;
1477 }
1478
1479 static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1480 {
1481 int i;
1482
1483 if (CLI_HELP_REQUESTED)
1484 return cli_arg_help(cli, 1,
1485 "all", "Enable debugging for all except \"data\"",
1486 "critical", "", // FIXME: add descriptions
1487 "error", "",
1488 "warning", "",
1489 "info", "",
1490 "calls", "",
1491 "data", "",
1492 NULL);
1493
1494 if (!argc)
1495 {
1496 char *p = (char *) &debug_flags;
1497 for (i = 0; i < sizeof(debug_flags); i++)
1498 {
1499 if (p[i])
1500 {
1501 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1502 (debug_flags.critical) ? " critical" : "",
1503 (debug_flags.error) ? " error" : "",
1504 (debug_flags.warning) ? " warning" : "",
1505 (debug_flags.info) ? " info" : "",
1506 (debug_flags.calls) ? " calls" : "",
1507 (debug_flags.data) ? " data" : "");
1508
1509 return CLI_OK;
1510 }
1511 }
1512
1513 cli_print(cli, "Debugging off");
1514 return CLI_OK;
1515 }
1516
1517 for (i = 0; i < argc; i++)
1518 {
1519 int len = strlen(argv[i]);
1520
1521 if (argv[i][0] == 'c' && len < 2)
1522 len = 2; /* distinguish [cr]itical from [ca]lls */
1523
1524 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 1; continue; }
1525 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 1; continue; }
1526 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 1; continue; }
1527 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 1; continue; }
1528 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 1; continue; }
1529 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 1; continue; }
1530 if (!strncasecmp(argv[i], "all", len))
1531 {
1532 memset(&debug_flags, 1, sizeof(debug_flags));
1533 debug_flags.data = 0;
1534 continue;
1535 }
1536
1537 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1538 }
1539
1540 return CLI_OK;
1541 }
1542
1543 static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1544 {
1545 int i;
1546
1547 if (CLI_HELP_REQUESTED)
1548 return cli_arg_help(cli, 1,
1549 "all", "Disable all debugging",
1550 "critical", "", // FIXME: add descriptions
1551 "error", "",
1552 "warning", "",
1553 "info", "",
1554 "calls", "",
1555 "data", "",
1556 NULL);
1557
1558 if (!argc)
1559 {
1560 memset(&debug_flags, 0, sizeof(debug_flags));
1561 return CLI_OK;
1562 }
1563
1564 for (i = 0; i < argc; i++)
1565 {
1566 int len = strlen(argv[i]);
1567
1568 if (argv[i][0] == 'c' && len < 2)
1569 len = 2; /* distinguish [cr]itical from [ca]lls */
1570
1571 if (!strncasecmp(argv[i], "critical", len)) { debug_flags.critical = 0; continue; }
1572 if (!strncasecmp(argv[i], "error", len)) { debug_flags.error = 0; continue; }
1573 if (!strncasecmp(argv[i], "warning", len)) { debug_flags.warning = 0; continue; }
1574 if (!strncasecmp(argv[i], "info", len)) { debug_flags.info = 0; continue; }
1575 if (!strncasecmp(argv[i], "calls", len)) { debug_flags.calls = 0; continue; }
1576 if (!strncasecmp(argv[i], "data", len)) { debug_flags.data = 0; continue; }
1577 if (!strncasecmp(argv[i], "all", len))
1578 {
1579 memset(&debug_flags, 0, sizeof(debug_flags));
1580 continue;
1581 }
1582
1583 cli_print(cli, "Invalid debugging flag \"%s\"", argv[i]);
1584 }
1585
1586 return CLI_OK;
1587 }
1588
1589 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1590 {
1591 int i, firstfree = 0;
1592
1593 if (CLI_HELP_REQUESTED)
1594 return cli_arg_help(cli, argc > 1,
1595 "PLUGIN", "Name of plugin to load", NULL);
1596
1597 if (argc != 1)
1598 {
1599 cli_print(cli, "Specify a plugin to load");
1600 return CLI_OK;
1601 }
1602
1603 for (i = 0; i < MAXPLUGINS; i++)
1604 {
1605 if (!*config->plugins[i] && !firstfree)
1606 firstfree = i;
1607 if (strcmp(config->plugins[i], argv[0]) == 0)
1608 {
1609 cli_print(cli, "Plugin is already loaded");
1610 return CLI_OK;
1611 }
1612 }
1613
1614 if (firstfree)
1615 {
1616 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1617 config->reload_config = 1;
1618 cli_print(cli, "Loading plugin %s", argv[0]);
1619 }
1620
1621 return CLI_OK;
1622 }
1623
1624 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1625 {
1626 int i;
1627
1628 if (CLI_HELP_REQUESTED)
1629 return cli_arg_help(cli, argc > 1,
1630 "PLUGIN", "Name of plugin to unload", NULL);
1631
1632 if (argc != 1)
1633 {
1634 cli_print(cli, "Specify a plugin to remove");
1635 return CLI_OK;
1636 }
1637
1638 for (i = 0; i < MAXPLUGINS; i++)
1639 {
1640 if (strcmp(config->plugins[i], argv[0]) == 0)
1641 {
1642 config->reload_config = 1;
1643 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1644 return CLI_OK;
1645 }
1646 }
1647
1648 cli_print(cli, "Plugin is not loaded");
1649 return CLI_OK;
1650 }
1651
1652 char *duration(time_t secs)
1653 {
1654 static char *buf = NULL;
1655 int p = 0;
1656
1657 if (!buf) buf = calloc(64, 1);
1658
1659 if (secs >= 86400)
1660 {
1661 int days = secs / 86400;
1662 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1663 secs %= 86400;
1664 }
1665
1666 if (secs >= 3600)
1667 {
1668 int mins = secs / 60;
1669 int hrs = mins / 60;
1670
1671 mins %= 60;
1672 sprintf(buf + p, "%d:%02d", hrs, mins);
1673 }
1674 else if (secs >= 60)
1675 {
1676 int mins = secs / 60;
1677 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1678 }
1679 else
1680 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1681
1682 return buf;
1683 }
1684
1685 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1686 {
1687 FILE *fh;
1688 char buf[100], *p = buf, *loads[3];
1689 int i, num_sessions = 0;
1690
1691 if (CLI_HELP_REQUESTED)
1692 return CLI_HELP_NO_ARGS;
1693
1694 fh = fopen("/proc/loadavg", "r");
1695 fgets(buf, 100, fh);
1696 fclose(fh);
1697
1698 for (i = 0; i < 3; i++)
1699 loads[i] = strdup(strsep(&p, " "));
1700
1701 time(&time_now);
1702 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1703
1704 for (i = 1; i < MAXSESSION; i++)
1705 if (session[i].opened) num_sessions++;
1706
1707 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1708 buf,
1709 duration(time_now - config->start_time),
1710 num_sessions,
1711 loads[0], loads[1], loads[2]
1712 );
1713 for (i = 0; i < 3; i++)
1714 if (loads[i]) free(loads[i]);
1715
1716 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1717
1718 return CLI_OK;
1719 }
1720
1721 static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1722 {
1723 int i;
1724
1725 if (CLI_HELP_REQUESTED)
1726 {
1727 switch (argc)
1728 {
1729 case 1:
1730 {
1731 int len = strlen(argv[0])-1;
1732 for (i = 0; config_values[i].key; i++)
1733 if (!len || !strncmp(argv[0], config_values[i].key, len))
1734 cli_print(cli, " %s", config_values[i].key);
1735 }
1736
1737 return CLI_OK;
1738
1739 case 2:
1740 return cli_arg_help(cli, 0,
1741 "VALUE", "Value for variable", NULL);
1742
1743 case 3:
1744 if (!argv[2][1])
1745 return cli_arg_help(cli, 1, NULL);
1746
1747 default:
1748 return CLI_OK;
1749 }
1750 }
1751
1752 if (argc != 2)
1753 {
1754 cli_print(cli, "Specify variable and value");
1755 return CLI_OK;
1756 }
1757
1758 for (i = 0; config_values[i].key; i++)
1759 {
1760 void *value = ((void *)config) + config_values[i].offset;
1761 if (strcmp(config_values[i].key, argv[0]) == 0)
1762 {
1763 // Found a value to set
1764 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1765 switch (config_values[i].type)
1766 {
1767 case STRING:
1768 strncpy((char *)value, argv[1], config_values[i].size - 1);
1769 break;
1770 case INT:
1771 *(int *)value = atoi(argv[1]);
1772 break;
1773 case UNSIGNED_LONG:
1774 *(unsigned long *)value = atol(argv[1]);
1775 break;
1776 case SHORT:
1777 *(short *)value = atoi(argv[1]);
1778 break;
1779 case IP:
1780 *(unsigned *)value = inet_addr(argv[1]);
1781 break;
1782 case MAC:
1783 parsemac(argv[1], (char *)value);
1784 break;
1785 case BOOL:
1786 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1787 *(int *)value = 1;
1788 else
1789 *(int *)value = 0;
1790 break;
1791 default:
1792 cli_print(cli, "Unknown variable type");
1793 break;
1794 }
1795 config->reload_config = 1;
1796 return CLI_OK;
1797 }
1798 }
1799
1800 cli_print(cli, "Unknown variable \"%s\"", argv[0]);
1801 return CLI_OK;
1802 }
1803
1804 int regular_stuff(struct cli_def *cli)
1805 {
1806 int i = debug_rb_tail;
1807 int reprompt = 0;
1808
1809 #ifdef RINGBUFFER
1810 while (i != ringbuffer->tail)
1811 {
1812 int show_message = 0;
1813
1814 if (*ringbuffer->buffer[i].message)
1815 {
1816 // Always show messages if we are doing general debug
1817 if (ringbuffer->buffer[i].level == 0 && debug_flags.critical) show_message = 1;
1818 if (ringbuffer->buffer[i].level == 1 && debug_flags.error) show_message = 1;
1819 if (ringbuffer->buffer[i].level == 2 && debug_flags.warning) show_message = 1;
1820 if (ringbuffer->buffer[i].level == 3 && debug_flags.info) show_message = 1;
1821 if (ringbuffer->buffer[i].level == 4 && debug_flags.calls) show_message = 1;
1822 if (ringbuffer->buffer[i].level == 5 && debug_flags.data) show_message = 1;
1823 }
1824
1825 if (show_message)
1826 {
1827 ipt address = htonl(ringbuffer->buffer[i].address);
1828 char *ipaddr;
1829 struct in_addr addr;
1830
1831 memcpy(&addr, &address, sizeof(ringbuffer->buffer[i].address));
1832 ipaddr = inet_ntoa(addr);
1833
1834 cli_print(cli, "\r%s-%s-%u-%u %s",
1835 debug_levels[(int)ringbuffer->buffer[i].level],
1836 ipaddr,
1837 ringbuffer->buffer[i].tunnel,
1838 ringbuffer->buffer[i].session,
1839 ringbuffer->buffer[i].message);
1840
1841 reprompt = 1;
1842 }
1843
1844 if (++i == ringbuffer->tail) break;
1845 if (i == RINGBUFFER_SIZE) i = 0;
1846 }
1847
1848 debug_rb_tail = ringbuffer->tail;
1849 if (reprompt)
1850 cli_reprompt(cli);
1851 #endif
1852 return CLI_OK;
1853 }
1854
1855 #ifdef BGP
1856 static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc)
1857 {
1858 int as;
1859
1860 if (CLI_HELP_REQUESTED)
1861 return cli_arg_help(cli, argc > 1,
1862 "<1-65535>", "Autonomous system number", NULL);
1863
1864 if (argc != 1 || (as = atoi(argv[0])) < 1 || as > 65535)
1865 {
1866 cli_print(cli, "Invalid autonomous system number");
1867 return CLI_OK;
1868 }
1869
1870 if (bgp_configured && as != config->as_number)
1871 {
1872 cli_print(cli, "Can't change local AS on a running system");
1873 return CLI_OK;
1874 }
1875
1876 config->as_number = as;
1877 cli_set_configmode(cli, MODE_CONFIG_BGP, "router");
1878
1879 return CLI_OK;
1880 }
1881
1882 static int cmd_router_bgp_exit(struct cli_def *cli, char *command, char **argv, int argc)
1883 {
1884 if (CLI_HELP_REQUESTED)
1885 return CLI_HELP_NO_ARGS;
1886
1887 cli_set_configmode(cli, MODE_CONFIG, NULL);
1888 return CLI_OK;
1889 }
1890
1891 static int find_bgp_neighbour(char *name)
1892 {
1893 int i;
1894 int new = -1;
1895 struct hostent *h;
1896 in_addr_t addrs[4] = { 0 };
1897 char **a;
1898
1899 if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET)
1900 return -2;
1901
1902 for (i = 0; i < sizeof(addrs) / sizeof(*addrs) && h->h_addr_list[i]; i++)
1903 memcpy(&addrs[i], h->h_addr_list[i], sizeof(*addrs));
1904
1905 for (i = 0; i < BGP_NUM_PEERS; i++)
1906 {
1907 if (!config->neighbour[i].name[0])
1908 {
1909 if (new == -1) new = i;
1910 continue;
1911 }
1912
1913 if (!strcmp(name, config->neighbour[i].name))
1914 return i;
1915
1916 if (!(h = gethostbyname(config->neighbour[i].name)) || h->h_addrtype != AF_INET)
1917 continue;
1918
1919 for (a = h->h_addr_list; *a; a++)
1920 {
1921 int j;
1922 for (j = 0; j < sizeof(addrs) / sizeof(*addrs) && addrs[j]; j++)
1923 if (!memcmp(&addrs[j], *a, sizeof(*addrs)))
1924 return i;
1925 }
1926 }
1927
1928 return new;
1929 }
1930
1931 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc)
1932 {
1933 int i;
1934 int keepalive;
1935 int hold;
1936
1937 if (CLI_HELP_REQUESTED)
1938 {
1939 switch (argc)
1940 {
1941 case 1:
1942 return cli_arg_help(cli, 0,
1943 "A.B.C.D", "BGP neighbour address",
1944 "NAME", "BGP neighbour name",
1945 NULL);
1946
1947 case 2:
1948 return cli_arg_help(cli, 0,
1949 "remote-as", "Set remote autonomous system number",
1950 "timers", "Set timers",
1951 NULL);
1952
1953 default:
1954 if (!strncmp("remote-as", argv[1], strlen(argv[1])))
1955 return cli_arg_help(cli, argv[2][1], "<1-65535>", "Autonomous system number", NULL);
1956
1957 if (!strncmp("timers", argv[1], strlen(argv[1])))
1958 {
1959 if (argc == 3)
1960 return cli_arg_help(cli, 0, "<1-65535>", "Keepalive time", NULL);
1961
1962 if (argc == 4)
1963 return cli_arg_help(cli, argv[3][1], "<3-65535>", "Hold time", NULL);
1964
1965 if (argc == 5 && !argv[4][1])
1966 return cli_arg_help(cli, 1, NULL);
1967 }
1968
1969 return CLI_OK;
1970 }
1971 }
1972
1973 if (argc < 3)
1974 {
1975 cli_print(cli, "Invalid arguments");
1976 return CLI_OK;
1977 }
1978
1979 if ((i = find_bgp_neighbour(argv[0])) == -2)
1980 {
1981 cli_print(cli, "Invalid neighbour");
1982 return CLI_OK;
1983 }
1984
1985 if (i == -1)
1986 {
1987 cli_print(cli, "Too many neighbours (max %d)", BGP_NUM_PEERS);
1988 return CLI_OK;
1989 }
1990
1991 if (!strncmp("remote-as", argv[1], strlen(argv[1])))
1992 {
1993 int as = atoi(argv[2]);
1994 if (as < 0 || as > 65535)
1995 {
1996 cli_print(cli, "Invalid autonomous system number");
1997 return CLI_OK;
1998 }
1999
2000 if (!config->neighbour[i].name[0])
2001 {
2002 snprintf(config->neighbour[i].name, sizeof(config->neighbour[i].name), argv[0]);
2003 config->neighbour[i].keepalive = -1;
2004 config->neighbour[i].hold = -1;
2005 }
2006
2007 config->neighbour[i].as = as;
2008 return CLI_OK;
2009 }
2010
2011 if (argc != 4 || strncmp("timers", argv[1], strlen(argv[1])))
2012 {
2013 cli_print(cli, "Invalid arguments");
2014 return CLI_OK;
2015 }
2016
2017 if (!config->neighbour[i].name[0])
2018 {
2019 cli_print(cli, "Specify remote-as first");
2020 return CLI_OK;
2021 }
2022
2023 keepalive = atoi(argv[2]);
2024 hold = atoi(argv[3]);
2025
2026 if (keepalive < 1 || keepalive > 65535)
2027 {
2028 cli_print(cli, "Invalid keepalive time");
2029 return CLI_OK;
2030 }
2031
2032 if (hold < 3 || hold > 65535)
2033 {
2034 cli_print(cli, "Invalid hold time");
2035 return CLI_OK;
2036 }
2037
2038 if (keepalive == BGP_KEEPALIVE_TIME)
2039 keepalive = -1; // using default value
2040
2041 if (hold == BGP_HOLD_TIME)
2042 hold = -1;
2043
2044 config->neighbour[i].keepalive = keepalive;
2045 config->neighbour[i].hold = hold;
2046
2047 return CLI_OK;
2048 }
2049
2050 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc)
2051 {
2052 int i;
2053
2054 if (CLI_HELP_REQUESTED)
2055 return cli_arg_help(cli, argc > 0,
2056 "A.B.C.D", "BGP neighbour address",
2057 "NAME", "BGP neighbour name",
2058 NULL);
2059
2060 if (argc != 1)
2061 {
2062 cli_print(cli, "Specify a BGP neighbour");
2063 return CLI_OK;
2064 }
2065
2066 if ((i = find_bgp_neighbour(argv[0])) == -2)
2067 {
2068 cli_print(cli, "Invalid neighbour");
2069 return CLI_OK;
2070 }
2071
2072 if (i < 0 || !config->neighbour[i].name[0])
2073 {
2074 cli_print(cli, "Neighbour %s not configured", argv[0]);
2075 return CLI_OK;
2076 }
2077
2078 memset(&config->neighbour[i], 0, sizeof(config->neighbour[i]));
2079 return CLI_OK;
2080 }
2081
2082 static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2083 {
2084 int i;
2085 int hdr = 0;
2086 char *addr;
2087
2088 if (!bgp_configured)
2089 return CLI_OK;
2090
2091 if (CLI_HELP_REQUESTED)
2092 return cli_arg_help(cli, 1,
2093 "A.B.C.D", "BGP neighbour address",
2094 "NAME", "BGP neighbour name",
2095 NULL);
2096
2097 cli_print(cli, "BGPv%d router identifier %s, local AS number %d",
2098 BGP_VERSION, inet_toa(my_address), (int) config->as_number);
2099
2100 time(&time_now);
2101
2102 for (i = 0; i < BGP_NUM_PEERS; i++)
2103 {
2104 if (!*bgp_peers[i].name)
2105 continue;
2106
2107 addr = inet_toa(bgp_peers[i].addr);
2108 if (argc && strcmp(addr, argv[0]) &&
2109 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2110 continue;
2111
2112 if (!hdr++)
2113 {
2114 cli_print(cli, "");
2115 cli_print(cli, "Peer AS Address "
2116 "State Retries Retry in Route Pend Timers");
2117 cli_print(cli, "------------------ ----- --------------- "
2118 "----------- ------- -------- ----- ---- ---------");
2119 }
2120
2121 cli_print(cli, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s %4d %4d",
2122 bgp_peers[i].name,
2123 bgp_peers[i].as,
2124 addr,
2125 bgp_state_str(bgp_peers[i].state),
2126 bgp_peers[i].retry_count,
2127 bgp_peers[i].retry_time ? bgp_peers[i].retry_time - time_now : 0,
2128 bgp_peers[i].routing ? "yes" : "no",
2129 bgp_peers[i].update_routes ? "yes" : "no",
2130 bgp_peers[i].keepalive,
2131 bgp_peers[i].hold);
2132 }
2133
2134 return CLI_OK;
2135 }
2136
2137 static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2138 {
2139 int i;
2140 char *addr;
2141
2142 if (!bgp_configured)
2143 return CLI_OK;
2144
2145 if (CLI_HELP_REQUESTED)
2146 return cli_arg_help(cli, 1,
2147 "A.B.C.D", "BGP neighbour address",
2148 "NAME", "BGP neighbour name",
2149 NULL);
2150
2151 for (i = 0; i < BGP_NUM_PEERS; i++)
2152 {
2153 if (bgp_peers[i].state != Established)
2154 continue;
2155
2156 if (!bgp_peers[i].routing)
2157 continue;
2158
2159 addr = inet_toa(bgp_peers[i].addr);
2160 if (argc && strcmp(addr, argv[0]) && strcmp(bgp_peers[i].name, argv[0]))
2161 continue;
2162
2163 bgp_peers[i].cli_flag = BGP_CLI_SUSPEND;
2164 cli_print(cli, "Suspending peer %s", bgp_peers[i].name);
2165 }
2166
2167 return CLI_OK;
2168 }
2169
2170 static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2171 {
2172 int i;
2173 char *addr;
2174
2175 if (!bgp_configured)
2176 return CLI_OK;
2177
2178 if (CLI_HELP_REQUESTED)
2179 return cli_arg_help(cli, 1,
2180 "A.B.C.D", "BGP neighbour address",
2181 "NAME", "BGP neighbour name",
2182 NULL);
2183
2184 for (i = 0; i < BGP_NUM_PEERS; i++)
2185 {
2186 if (bgp_peers[i].state != Established)
2187 continue;
2188
2189 if (bgp_peers[i].routing)
2190 continue;
2191
2192 addr = inet_toa(bgp_peers[i].addr);
2193 if (argc && strcmp(addr, argv[0]) &&
2194 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2195 continue;
2196
2197 bgp_peers[i].cli_flag = BGP_CLI_ENABLE;
2198 cli_print(cli, "Un-suspending peer %s", bgp_peers[i].name);
2199 }
2200
2201 return CLI_OK;
2202 }
2203
2204 static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2205 {
2206 int i;
2207 char *addr;
2208
2209 if (!bgp_configured)
2210 return CLI_OK;
2211
2212 if (CLI_HELP_REQUESTED)
2213 return cli_arg_help(cli, 1,
2214 "A.B.C.D", "BGP neighbour address",
2215 "NAME", "BGP neighbour name",
2216 NULL);
2217
2218 for (i = 0; i < BGP_NUM_PEERS; i++)
2219 {
2220 if (!*bgp_peers[i].name)
2221 continue;
2222
2223 addr = inet_toa(bgp_peers[i].addr);
2224 if (argc && strcmp(addr, argv[0]) &&
2225 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2226 continue;
2227
2228 bgp_peers[i].cli_flag = BGP_CLI_RESTART;
2229 cli_print(cli, "Restarting peer %s", bgp_peers[i].name);
2230 }
2231
2232 return CLI_OK;
2233 }
2234 #endif /* BGP*/
2235
2236 // Convert a string in the form of abcd.ef12.3456 into char[6]
2237 void parsemac(char *string, char mac[6])
2238 {
2239 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)
2240 return;
2241 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)
2242 return;
2243 memset(mac, 0, 6);
2244 }