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