oop
[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.28 2004/11/16 07:54:32 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 static struct cli_def *cli = NULL;
41 extern struct configt *config;
42 extern struct config_descriptt config_values[];
43 #ifdef RINGBUFFER
44 extern struct Tringbuffer *ringbuffer;
45 #endif
46 extern struct cli_session_actions *cli_session_actions;
47 extern struct cli_tunnel_actions *cli_tunnel_actions;
48 extern tbft *filter_list;
49
50 static char *debug_levels[] = {
51 "CRIT",
52 "ERROR",
53 "WARN",
54 "INFO",
55 "CALL",
56 "DATA",
57 };
58
59 struct
60 {
61 char critical;
62 char error;
63 char warning;
64 char info;
65 char calls;
66 char data;
67 } debug_flags;
68
69 static int debug_session;
70 static int debug_tunnel;
71 static int debug_rb_tail;
72
73 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
74 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
75 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
76 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
77 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
78 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
79 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
80 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
81 static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
82 static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
83 static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc);
84 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
85 static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
86 static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
87 static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
88 static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
89 static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
90 static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
91 static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
92 static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
93 static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
94 static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
95 static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
96 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
97 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
98 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
99 static int regular_stuff(struct cli_def *cli);
100 static void parsemac(char *string, char mac[6]);
101
102 #ifdef BGP
103 #define MODE_CONFIG_BGP 8
104 static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc);
105 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
106 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
107 static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc);
108 static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
109 static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
110 static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc);
111 #endif /* BGP */
112
113 void init_cli(char *hostname)
114 {
115 FILE *f;
116 char buf[4096];
117 struct cli_command *c;
118 struct cli_command *c2;
119 int on = 1;
120 struct sockaddr_in addr;
121
122 cli = cli_init();
123 if (hostname && *hostname)
124 cli_set_hostname(cli, hostname);
125 else
126 cli_set_hostname(cli, "l2tpns");
127
128 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
129 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
130 #ifdef BGP
131 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
132 #endif /* BGP */
133 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
134 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
135 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
136 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
137 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
138 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the currently running configuration");
139 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
140 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
141 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
142 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
143 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");
144 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
145
146 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
147 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
148 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
149
150 #ifdef STATISTICS
151 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
152
153 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
154 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
155 #endif
156
157 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
158
159 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
160 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
161 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
162
163 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable interception for a user");
164 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily enable throttling for a user");
165 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
166
167 #ifdef BGP
168 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
169 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP neighbour");
170 #endif /* BGP */
171
172 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
173 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable interception for a user");
174 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Temporarily disable throttling for a user");
175 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
176
177 #ifdef BGP
178 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
179 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP neighbour");
180
181 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
182 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
183
184 c = cli_register_command(cli, NULL, "router", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
185 cli_register_command(cli, c, "bgp", cmd_router_bgp, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Configure BGP");
186
187 cli_register_command(cli, NULL, "neighbour", cmd_router_bgp_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Configure BGP neighbour");
188
189 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, NULL);
190 cli_register_command(cli, c, "neighbour", cmd_router_bgp_no_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Remove BGP neighbour");
191 #endif /* BGP */
192
193 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
194 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
195 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
196 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
197
198 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
199 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
200
201 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
202 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
203
204 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
205
206 // Enable regular processing
207 cli_regular(cli, regular_stuff);
208
209 if (!(f = fopen(CLIUSERS, "r")))
210 {
211 LOG(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
212 }
213 else
214 {
215 while (fgets(buf, 4096, f))
216 {
217 char *p;
218 if (*buf == '#') continue;
219 if ((p = strchr(buf, '\r'))) *p = 0;
220 if ((p = strchr(buf, '\n'))) *p = 0;
221 if (!*buf) continue;
222 if (!(p = strchr((char *)buf, ':'))) continue;
223 *p++ = 0;
224 if (!strcmp(buf, "enable"))
225 {
226 cli_allow_enable(cli, p);
227 LOG(3, 0, 0, 0, "Setting enable password\n");
228 }
229 else
230 {
231 cli_allow_user(cli, buf, p);
232 LOG(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
233 }
234 }
235 fclose(f);
236 }
237
238 memset(&addr, 0, sizeof(addr));
239 clifd = socket(PF_INET, SOCK_STREAM, 6);
240 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
241 {
242 int flags;
243 // Set cli fd as non-blocking
244 flags = fcntl(clifd, F_GETFL, 0);
245 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
246 }
247 addr.sin_family = AF_INET;
248 addr.sin_port = htons(23);
249 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
250 {
251 LOG(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
252 return;
253 }
254 listen(clifd, 10);
255 }
256
257 void cli_do(int sockfd)
258 {
259 int require_auth = 1;
260 struct sockaddr_in addr;
261 int l = sizeof(addr);
262
263 if (fork_and_close()) return;
264 if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0)
265 {
266 LOG(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr.sin_addr.s_addr));
267 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
268 }
269 else
270 LOG(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
271
272 if (require_auth)
273 {
274 LOG(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
275 if (!cli->users) /* paranoia */
276 {
277 LOG(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
278 exit(0);
279 }
280 }
281 else
282 {
283 /* no username/pass required */
284 cli->users = 0;
285 }
286
287 debug_session = 0;
288 debug_tunnel = 0;
289 #ifdef RINGBUFFER
290 debug_rb_tail = ringbuffer->tail;
291 #endif
292 memset(&debug_flags, 0, sizeof(debug_flags));
293 debug_flags.critical = 1;
294
295 cli_loop(cli, sockfd);
296
297 close(sockfd);
298 LOG(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr.sin_addr.s_addr));
299 exit(0);
300 }
301
302 static void cli_print_log(struct cli_def *cli, char *string)
303 {
304 LOG(3, 0, 0, 0, "%s\n", string);
305 }
306
307 void cli_do_file(FILE *fh)
308 {
309 LOG(3, 0, 0, 0, "Reading configuration file\n");
310 cli_print_callback(cli, cli_print_log);
311 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
312 cli_print_callback(cli, NULL);
313 }
314
315 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
316 {
317 va_list ap;
318 char *desc;
319 char buf[16];
320 char *p;
321
322 va_start(ap, entry);
323 while (entry)
324 {
325 /* allow one %d */
326 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
327 {
328 int v = va_arg(ap, int);
329 snprintf(buf, sizeof(buf), entry, v);
330 p = buf;
331 }
332 else
333 p = entry;
334
335 desc = va_arg(ap, char *);
336 if (desc && *desc)
337 cli_print(cli, " %-20s %s", p, desc);
338 else
339 cli_print(cli, " %s", p);
340
341 entry = desc ? va_arg(ap, char *) : 0;
342 }
343
344 va_end(ap);
345 if (cr_ok)
346 cli_print(cli, " <cr>");
347
348 return CLI_OK;
349 }
350
351 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
352 {
353 int i;
354
355 if (CLI_HELP_REQUESTED)
356 return cli_arg_help(cli, 1,
357 "<1-%d>", MAXSESSION-1, "Show specific session by id",
358 NULL);
359
360 time(&time_now);
361 if (argc > 0)
362 {
363 // Show individual session
364 for (i = 0; i < argc; i++)
365 {
366 unsigned int s, b_in, b_out;
367 s = atoi(argv[i]);
368 if (s <= 0 || s >= MAXSESSION)
369 {
370 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
371 continue;
372 }
373 cli_print(cli, "\r\nSession %d:", s);
374 cli_print(cli, "\tUser:\t\t%s", session[s].user[0] ? session[s].user : "none");
375 cli_print(cli, "\tCalling Num:\t%s", session[s].calling);
376 cli_print(cli, "\tCalled Num:\t%s", session[s].called);
377 cli_print(cli, "\tTunnel ID:\t%d", session[s].tunnel);
378 cli_print(cli, "\tIP address:\t%s", inet_toa(htonl(session[s].ip)));
379 cli_print(cli, "\tUnique SID:\t%lu", session[s].unique_id);
380 cli_print(cli, "\tIdle time:\t%u seconds", abs(time_now - session[s].last_packet));
381 cli_print(cli, "\tNext Recv:\t%u", session[s].nr);
382 cli_print(cli, "\tNext Send:\t%u", session[s].ns);
383 cli_print(cli, "\tBytes In/Out:\t%lu/%lu", (unsigned long)session[s].total_cout, (unsigned long)session[s].total_cin);
384 cli_print(cli, "\tPkts In/Out:\t%lu/%lu", (unsigned long)session[s].pout, (unsigned long)session[s].pin);
385 cli_print(cli, "\tMRU:\t\t%d", session[s].mru);
386 cli_print(cli, "\tRadius Session:\t%u", session[s].radius);
387 cli_print(cli, "\tRx Speed:\t%lu", session[s].rx_connect_speed);
388 cli_print(cli, "\tTx Speed:\t%lu", session[s].tx_connect_speed);
389 if (session[s].snoop_ip && session[s].snoop_port)
390 cli_print(cli, "\tIntercepted:\t%s:%d", inet_toa(session[s].snoop_ip), session[s] .snoop_port);
391 else
392 cli_print(cli, "\tIntercepted:\tno");
393
394 cli_print(cli, "\tWalled Garden:\t%s", session[s].walled_garden ? "YES" : "no");
395 {
396 int t = (session[s].throttle_in || session[s].throttle_out);
397 cli_print(cli, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
398 t ? "YES" : "no", t ? " (" : "",
399 session[s].throttle_in, session[s].throttle_in ? "kbps" : t ? "-" : "",
400 t ? "/" : "",
401 session[s].throttle_out, session[s].throttle_out ? "kbps" : t ? "-" : "",
402 t ? ")" : "");
403 }
404
405 b_in = session[s].tbf_in;
406 b_out = session[s].tbf_out;
407 if (b_in || b_out)
408 cli_print(cli, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
409 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
410 "ByteSent", "PackSent", "PackDrop", "PackDelay");
411
412 if (b_in)
413 cli_print(cli, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
414 b_in,
415 (filter_list[b_in].next ? "*" : " "),
416 (b_in < 100 ? "\t" : ""),
417 filter_list[b_in].rate * 8,
418 filter_list[b_in].credit,
419 filter_list[b_in].queued,
420 filter_list[b_in].b_queued,
421 filter_list[b_in].p_queued,
422 filter_list[b_in].b_sent,
423 filter_list[b_in].p_sent,
424 filter_list[b_in].p_dropped,
425 filter_list[b_in].p_delayed);
426
427 if (b_out)
428 cli_print(cli, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
429 b_out,
430 (filter_list[b_out].next ? "*" : " "),
431 (b_out < 100 ? "\t" : ""),
432 filter_list[b_out].rate * 8,
433 filter_list[b_out].credit,
434 filter_list[b_out].queued,
435 filter_list[b_out].b_queued,
436 filter_list[b_out].p_queued,
437 filter_list[b_out].b_sent,
438 filter_list[b_out].p_sent,
439 filter_list[b_out].p_dropped,
440 filter_list[b_out].p_delayed);
441
442 }
443 return CLI_OK;
444 }
445
446 // Show Summary
447 cli_print(cli, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
448 "SID",
449 "TID",
450 "Username",
451 "IP",
452 "I",
453 "T",
454 "G",
455 "opened",
456 "downloaded",
457 "uploaded",
458 "idle",
459 "LAC",
460 "CLI");
461
462 for (i = 1; i < MAXSESSION; i++)
463 {
464 char *userip, *tunnelip;
465 if (!session[i].opened) continue;
466 userip = strdup(inet_toa(htonl(session[i].ip)));
467 tunnelip = strdup(inet_toa(htonl(tunnel[ session[i].tunnel ].ip)));
468 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
469 i,
470 session[i].tunnel,
471 session[i].user[0] ? session[i].user : "*",
472 userip,
473 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
474 (session[i].throttle_in || session[i].throttle_out) ? "Y" : "N",
475 (session[i].walled_garden) ? "Y" : "N",
476 abs(time_now - (unsigned long)session[i].opened),
477 (unsigned long)session[i].total_cout,
478 (unsigned long)session[i].total_cin,
479 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
480 tunnelip,
481 session[i].calling[0] ? session[i].calling : "*");
482 if (userip) free(userip);
483 if (tunnelip) free(tunnelip);
484 }
485 return CLI_OK;
486 }
487
488 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
489 {
490 int i, x, show_all = 0;
491 char *states[] = {
492 "Free",
493 "Open",
494 "Closing",
495 "Opening",
496 };
497
498 if (CLI_HELP_REQUESTED)
499 {
500 if (argc > 1)
501 return cli_arg_help(cli, 1,
502 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
503 NULL);
504
505 return cli_arg_help(cli, 1,
506 "all", "Show all tunnels, including unused",
507 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
508 NULL);
509 }
510
511 time(&time_now);
512 if (argc > 0)
513 {
514 if (strcmp(argv[0], "all") == 0)
515 {
516 show_all = 1;
517 }
518 else
519 {
520 // Show individual tunnel
521 for (i = 0; i < argc; i++)
522 {
523 char s[65535] = {0};
524 unsigned int t;
525 t = atoi(argv[i]);
526 if (t <= 0 || t >= MAXTUNNEL)
527 {
528 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
529 continue;
530 }
531 cli_print(cli, "\r\nTunnel %d:", t);
532 cli_print(cli, "\tState:\t\t%s", states[tunnel[t].state]);
533 cli_print(cli, "\tHostname:\t%s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
534 cli_print(cli, "\tRemote IP:\t%s", inet_toa(htonl(tunnel[t].ip)));
535 cli_print(cli, "\tRemote Port:\t%d", tunnel[t].port);
536 cli_print(cli, "\tRx Window:\t%u", tunnel[t].window);
537 cli_print(cli, "\tNext Recv:\t%u", tunnel[t].nr);
538 cli_print(cli, "\tNext Send:\t%u", tunnel[t].ns);
539 cli_print(cli, "\tQueue Len:\t%u", tunnel[t].controlc);
540 cli_print(cli, "\tLast Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
541
542 for (x = 0; x < MAXSESSION; x++)
543 if (session[x].tunnel == t && session[x].opened && !session[x].die)
544 sprintf(s, "%s%u ", s, x);
545
546 cli_print(cli, "\tSessions:\t%s", s);
547 }
548 return CLI_OK;
549 }
550 }
551
552 // Show tunnel summary
553 cli_print(cli, "%4s %20s %20s %6s %s",
554 "TID",
555 "Hostname",
556 "IP",
557 "State",
558 "Sessions");
559
560 for (i = 1; i < MAXTUNNEL; i++)
561 {
562 int sessions = 0;
563 if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
564
565 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
566 cli_print(cli, "%4d %20s %20s %6s %6d",
567 i,
568 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
569 inet_toa(htonl(tunnel[i].ip)),
570 states[tunnel[i].state],
571 sessions);
572 }
573
574 return CLI_OK;
575 }
576
577 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
578 {
579 char sid[32][8];
580 char *sargv[32];
581 int sargc = 0;
582 int i;
583
584 if (CLI_HELP_REQUESTED)
585 return cli_arg_help(cli, 1,
586 "USER", "Show details for specific username",
587 NULL);
588
589 for (i = 0; i < MAXSESSION; i++)
590 {
591 if (!session[i].opened) continue;
592 if (!session[i].user[0]) continue;
593 if (argc > 0)
594 {
595 int j;
596 for (j = 0; j < argc && sargc < 32; j++)
597 {
598 if (strcmp(argv[j], session[i].user) == 0)
599 {
600 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
601 sargv[sargc] = sid[sargc];
602 sargc++;
603 }
604 }
605
606 continue;
607 }
608
609 cli_print(cli, "%s", session[i].user);
610 }
611
612 if (sargc > 0)
613 return cmd_show_session(cli, "users", sargv, sargc);
614
615 return CLI_OK;
616 }
617
618 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
619 {
620 if (CLI_HELP_REQUESTED)
621 return CLI_HELP_NO_ARGS;
622
623 cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
624 cli_print(cli, "%-10s %8lu %8lu %8lu", "RX",
625 GET_STAT(tun_rx_bytes),
626 GET_STAT(tun_rx_packets),
627 GET_STAT(tun_rx_errors));
628 cli_print(cli, "%-10s %8lu %8lu %8lu", "TX",
629 GET_STAT(tun_tx_bytes),
630 GET_STAT(tun_tx_packets),
631 GET_STAT(tun_tx_errors));
632 cli_print(cli, "");
633
634 cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
635 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "RX",
636 GET_STAT(tunnel_rx_bytes),
637 GET_STAT(tunnel_rx_packets),
638 GET_STAT(tunnel_rx_errors),
639 0L);
640 cli_print(cli, "%-10s %8lu %8lu %8lu %8lu", "TX",
641 GET_STAT(tunnel_tx_bytes),
642 GET_STAT(tunnel_tx_packets),
643 GET_STAT(tunnel_tx_errors),
644 GET_STAT(tunnel_retries));
645 cli_print(cli, "");
646
647 cli_print(cli, "%-30s%-10s", "Counter", "Value");
648 cli_print(cli, "-----------------------------------------");
649 cli_print(cli, "%-30s%lu", "radius_retries", GET_STAT(radius_retries));
650 cli_print(cli, "%-30s%lu", "arp_sent", GET_STAT(arp_sent));
651 cli_print(cli, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped));
652 cli_print(cli, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created));
653 cli_print(cli, "%-30s%lu", "session_created", GET_STAT(session_created));
654 cli_print(cli, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout));
655 cli_print(cli, "%-30s%lu", "session_timeout", GET_STAT(session_timeout));
656 cli_print(cli, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout));
657 cli_print(cli, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow));
658 cli_print(cli, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow));
659 cli_print(cli, "%-30s%lu", "session_overflow", GET_STAT(session_overflow));
660 cli_print(cli, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated));
661 cli_print(cli, "%-30s%lu", "ip_freed", GET_STAT(ip_freed));
662 cli_print(cli, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded));
663 cli_print(cli, "%-30s%lu", "recv_forward", GET_STAT(recv_forward));
664
665
666 #ifdef STATISTICS
667 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
668 cli_print(cli, "-----------------------------------------");
669 cli_print(cli, "%-30s%lu", "call_processtun", GET_STAT(call_processtun));
670 cli_print(cli, "%-30s%lu", "call_processipout", GET_STAT(call_processipout));
671 cli_print(cli, "%-30s%lu", "call_processudp", GET_STAT(call_processudp));
672 cli_print(cli, "%-30s%lu", "call_processpap", GET_STAT(call_processpap));
673 cli_print(cli, "%-30s%lu", "call_processchap", GET_STAT(call_processchap));
674 cli_print(cli, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp));
675 cli_print(cli, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp));
676 cli_print(cli, "%-30s%lu", "call_processipin", GET_STAT(call_processipin));
677 cli_print(cli, "%-30s%lu", "call_processccp", GET_STAT(call_processccp));
678 cli_print(cli, "%-30s%lu", "call_processrad", GET_STAT(call_processrad));
679 cli_print(cli, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp));
680 cli_print(cli, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp));
681 cli_print(cli, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap));
682 cli_print(cli, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip));
683 cli_print(cli, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
684 cli_print(cli, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend));
685 cli_print(cli, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill));
686 cli_print(cli, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
687 cli_print(cli, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill));
688 cli_print(cli, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
689 cli_print(cli, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup));
690 cli_print(cli, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address));
691 cli_print(cli, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address));
692 cli_print(cli, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
693 cli_print(cli, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend));
694 cli_print(cli, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry));
695 #endif
696 return CLI_OK;
697 }
698
699 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
700 {
701 int tag = 0;
702 int file = 0;
703 int i = 0;
704
705 if (CLI_HELP_REQUESTED)
706 return cli_arg_help(cli, 1,
707 "tag", "Include CVS release tag",
708 "file", "Include file versions",
709 NULL);
710
711 for (i = 0; i < argc; i++)
712 if (!strcmp(argv[i], "tag"))
713 tag++;
714 else if (!strcmp(argv[i], "file"))
715 file++;
716
717 cli_print(cli, "L2TPNS %s", VERSION);
718 if (tag)
719 {
720 char const *p = strchr(cvs_name, ':');
721 char const *e;
722 if (p)
723 {
724 p++;
725 while (isspace(*p))
726 p++;
727 }
728
729 if (!p || *p == '$')
730 p = "HEAD";
731
732 e = strpbrk(p, " \t$");
733 cli_print(cli, "Tag: %.*s", e ? e - p + 1 : strlen(p), p);
734 }
735
736 if (file)
737 {
738 extern linked_list *loaded_plugins;
739 void *p;
740
741 cli_print(cli, "Files:");
742 cli_print(cli, " %s", cvs_id_arp);
743 #ifdef BGP
744 cli_print(cli, " %s", cvs_id_bgp);
745 #endif /* BGP */
746 cli_print(cli, " %s", cvs_id_cli);
747 cli_print(cli, " %s", cvs_id_cluster);
748 cli_print(cli, " %s", cvs_id_constants);
749 cli_print(cli, " %s", cvs_id_control);
750 cli_print(cli, " %s", cvs_id_icmp);
751 cli_print(cli, " %s", cvs_id_l2tpns);
752 cli_print(cli, " %s", cvs_id_ll);
753 cli_print(cli, " %s", cvs_id_md5);
754 cli_print(cli, " %s", cvs_id_ppp);
755 cli_print(cli, " %s", cvs_id_radius);
756 cli_print(cli, " %s", cvs_id_tbf);
757 cli_print(cli, " %s", cvs_id_util);
758
759 ll_reset(loaded_plugins);
760 while ((p = ll_next(loaded_plugins)))
761 {
762 char const **id = dlsym(p, "cvs_id");
763 if (id)
764 cli_print(cli, " %s", *id);
765 }
766 }
767
768 return CLI_OK;
769 }
770
771 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
772 {
773 int i;
774 int used = 0, free = 0, show_all = 0;
775
776 if (!config->cluster_iam_master)
777 {
778 cli_print(cli, "Can't do this on a slave. Do it on %s", inet_toa(config->cluster_master_address));
779 return CLI_OK;
780 }
781
782 if (CLI_HELP_REQUESTED)
783 {
784 if (argc > 1)
785 return cli_arg_help(cli, 1, NULL);
786
787 return cli_arg_help(cli, 1,
788 "all", "Show all pool addresses, including unused",
789 NULL);
790 }
791
792 if (argc > 0 && strcmp(argv[0], "all") == 0)
793 show_all = 1;
794
795 time(&time_now);
796 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
797 for (i = 0; i < MAXIPPOOL; i++)
798 {
799 if (!ip_address_pool[i].address) continue;
800 if (ip_address_pool[i].assigned)
801 {
802 cli_print(cli, "%-15s\tY %8d %s",
803 inet_toa(htonl(ip_address_pool[i].address)), ip_address_pool[i].session, session[ip_address_pool[i].session].user);
804
805 used++;
806 }
807 else
808 {
809 if (ip_address_pool[i].last)
810 cli_print(cli, "%-15s\tN %8s [%s] %ds",
811 inet_toa(htonl(ip_address_pool[i].address)), "",
812 ip_address_pool[i].user, time_now - ip_address_pool[i].last);
813 else if (show_all)
814 cli_print(cli, "%-15s\tN", inet_toa(htonl(ip_address_pool[i].address)));
815
816 free++;
817 }
818 }
819
820 if (!show_all)
821 cli_print(cli, "(Not displaying unused addresses)");
822
823 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
824 return CLI_OK;
825 }
826
827 static FILE *save_config_fh = 0;
828 static void print_save_config(struct cli_def *cli, char *string)
829 {
830 if (save_config_fh)
831 fprintf(save_config_fh, "%s\n", string);
832 }
833
834 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
835 {
836 if (CLI_HELP_REQUESTED)
837 return CLI_HELP_NO_ARGS;
838
839 if ((save_config_fh = fopen(config->config_file, "w")))
840 {
841 cli_print(cli, "Writing configuration");
842 cli_print_callback(cli, print_save_config);
843 cmd_show_run(cli, command, argv, argc);
844 cli_print_callback(cli, NULL);
845 fclose(save_config_fh);
846 save_config_fh = 0;
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 static 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 find_bgp_neighbour(char *name)
1881 {
1882 int i;
1883 int new = -1;
1884 struct hostent *h;
1885 in_addr_t addrs[4] = { 0 };
1886 char **a;
1887
1888 if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET)
1889 return -2;
1890
1891 for (i = 0; i < sizeof(addrs) / sizeof(*addrs) && h->h_addr_list[i]; i++)
1892 memcpy(&addrs[i], h->h_addr_list[i], sizeof(*addrs));
1893
1894 for (i = 0; i < BGP_NUM_PEERS; i++)
1895 {
1896 if (!config->neighbour[i].name[0])
1897 {
1898 if (new == -1) new = i;
1899 continue;
1900 }
1901
1902 if (!strcmp(name, config->neighbour[i].name))
1903 return i;
1904
1905 if (!(h = gethostbyname(config->neighbour[i].name)) || h->h_addrtype != AF_INET)
1906 continue;
1907
1908 for (a = h->h_addr_list; *a; a++)
1909 {
1910 int j;
1911 for (j = 0; j < sizeof(addrs) / sizeof(*addrs) && addrs[j]; j++)
1912 if (!memcmp(&addrs[j], *a, sizeof(*addrs)))
1913 return i;
1914 }
1915 }
1916
1917 return new;
1918 }
1919
1920 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc)
1921 {
1922 int i;
1923 int keepalive;
1924 int hold;
1925
1926 if (CLI_HELP_REQUESTED)
1927 {
1928 switch (argc)
1929 {
1930 case 1:
1931 return cli_arg_help(cli, 0,
1932 "A.B.C.D", "BGP neighbour address",
1933 "NAME", "BGP neighbour name",
1934 NULL);
1935
1936 case 2:
1937 return cli_arg_help(cli, 0,
1938 "remote-as", "Set remote autonomous system number",
1939 "timers", "Set timers",
1940 NULL);
1941
1942 default:
1943 if (!strncmp("remote-as", argv[1], strlen(argv[1])))
1944 return cli_arg_help(cli, argv[2][1], "<1-65535>", "Autonomous system number", NULL);
1945
1946 if (!strncmp("timers", argv[1], strlen(argv[1])))
1947 {
1948 if (argc == 3)
1949 return cli_arg_help(cli, 0, "<1-65535>", "Keepalive time", NULL);
1950
1951 if (argc == 4)
1952 return cli_arg_help(cli, argv[3][1], "<3-65535>", "Hold time", NULL);
1953
1954 if (argc == 5 && !argv[4][1])
1955 return cli_arg_help(cli, 1, NULL);
1956 }
1957
1958 return CLI_OK;
1959 }
1960 }
1961
1962 if (argc < 3)
1963 {
1964 cli_print(cli, "Invalid arguments");
1965 return CLI_OK;
1966 }
1967
1968 if ((i = find_bgp_neighbour(argv[0])) == -2)
1969 {
1970 cli_print(cli, "Invalid neighbour");
1971 return CLI_OK;
1972 }
1973
1974 if (i == -1)
1975 {
1976 cli_print(cli, "Too many neighbours (max %d)", BGP_NUM_PEERS);
1977 return CLI_OK;
1978 }
1979
1980 if (!strncmp("remote-as", argv[1], strlen(argv[1])))
1981 {
1982 int as = atoi(argv[2]);
1983 if (as < 0 || as > 65535)
1984 {
1985 cli_print(cli, "Invalid autonomous system number");
1986 return CLI_OK;
1987 }
1988
1989 if (!config->neighbour[i].name[0])
1990 {
1991 snprintf(config->neighbour[i].name, sizeof(config->neighbour[i].name), argv[0]);
1992 config->neighbour[i].keepalive = -1;
1993 config->neighbour[i].hold = -1;
1994 }
1995
1996 config->neighbour[i].as = as;
1997 return CLI_OK;
1998 }
1999
2000 if (argc != 4 || strncmp("timers", argv[1], strlen(argv[1])))
2001 {
2002 cli_print(cli, "Invalid arguments");
2003 return CLI_OK;
2004 }
2005
2006 if (!config->neighbour[i].name[0])
2007 {
2008 cli_print(cli, "Specify remote-as first");
2009 return CLI_OK;
2010 }
2011
2012 keepalive = atoi(argv[2]);
2013 hold = atoi(argv[3]);
2014
2015 if (keepalive < 1 || keepalive > 65535)
2016 {
2017 cli_print(cli, "Invalid keepalive time");
2018 return CLI_OK;
2019 }
2020
2021 if (hold < 3 || hold > 65535)
2022 {
2023 cli_print(cli, "Invalid hold time");
2024 return CLI_OK;
2025 }
2026
2027 if (keepalive == BGP_KEEPALIVE_TIME)
2028 keepalive = -1; // using default value
2029
2030 if (hold == BGP_HOLD_TIME)
2031 hold = -1;
2032
2033 config->neighbour[i].keepalive = keepalive;
2034 config->neighbour[i].hold = hold;
2035
2036 return CLI_OK;
2037 }
2038
2039 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc)
2040 {
2041 int i;
2042
2043 if (CLI_HELP_REQUESTED)
2044 return cli_arg_help(cli, argc > 0,
2045 "A.B.C.D", "BGP neighbour address",
2046 "NAME", "BGP neighbour name",
2047 NULL);
2048
2049 if (argc != 1)
2050 {
2051 cli_print(cli, "Specify a BGP neighbour");
2052 return CLI_OK;
2053 }
2054
2055 if ((i = find_bgp_neighbour(argv[0])) == -2)
2056 {
2057 cli_print(cli, "Invalid neighbour");
2058 return CLI_OK;
2059 }
2060
2061 if (i < 0 || !config->neighbour[i].name[0])
2062 {
2063 cli_print(cli, "Neighbour %s not configured", argv[0]);
2064 return CLI_OK;
2065 }
2066
2067 memset(&config->neighbour[i], 0, sizeof(config->neighbour[i]));
2068 return CLI_OK;
2069 }
2070
2071 static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2072 {
2073 int i;
2074 int hdr = 0;
2075 char *addr;
2076
2077 if (!bgp_configured)
2078 return CLI_OK;
2079
2080 if (CLI_HELP_REQUESTED)
2081 return cli_arg_help(cli, 1,
2082 "A.B.C.D", "BGP neighbour address",
2083 "NAME", "BGP neighbour name",
2084 NULL);
2085
2086 cli_print(cli, "BGPv%d router identifier %s, local AS number %d",
2087 BGP_VERSION, inet_toa(my_address), (int) config->as_number);
2088
2089 time(&time_now);
2090
2091 for (i = 0; i < BGP_NUM_PEERS; i++)
2092 {
2093 if (!*bgp_peers[i].name)
2094 continue;
2095
2096 addr = inet_toa(bgp_peers[i].addr);
2097 if (argc && strcmp(addr, argv[0]) &&
2098 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2099 continue;
2100
2101 if (!hdr++)
2102 {
2103 cli_print(cli, "");
2104 cli_print(cli, "Peer AS Address "
2105 "State Retries Retry in Route Pend Timers");
2106 cli_print(cli, "------------------ ----- --------------- "
2107 "----------- ------- -------- ----- ---- ---------");
2108 }
2109
2110 cli_print(cli, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s %4d %4d",
2111 bgp_peers[i].name,
2112 bgp_peers[i].as,
2113 addr,
2114 bgp_state_str(bgp_peers[i].state),
2115 bgp_peers[i].retry_count,
2116 bgp_peers[i].retry_time ? bgp_peers[i].retry_time - time_now : 0,
2117 bgp_peers[i].routing ? "yes" : "no",
2118 bgp_peers[i].update_routes ? "yes" : "no",
2119 bgp_peers[i].keepalive,
2120 bgp_peers[i].hold);
2121 }
2122
2123 return CLI_OK;
2124 }
2125
2126 static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2127 {
2128 int i;
2129 char *addr;
2130
2131 if (!bgp_configured)
2132 return CLI_OK;
2133
2134 if (CLI_HELP_REQUESTED)
2135 return cli_arg_help(cli, 1,
2136 "A.B.C.D", "BGP neighbour address",
2137 "NAME", "BGP neighbour name",
2138 NULL);
2139
2140 for (i = 0; i < BGP_NUM_PEERS; i++)
2141 {
2142 if (bgp_peers[i].state != Established)
2143 continue;
2144
2145 if (!bgp_peers[i].routing)
2146 continue;
2147
2148 addr = inet_toa(bgp_peers[i].addr);
2149 if (argc && strcmp(addr, argv[0]) && strcmp(bgp_peers[i].name, argv[0]))
2150 continue;
2151
2152 bgp_peers[i].cli_flag = BGP_CLI_SUSPEND;
2153 cli_print(cli, "Suspending peer %s", bgp_peers[i].name);
2154 }
2155
2156 return CLI_OK;
2157 }
2158
2159 static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2160 {
2161 int i;
2162 char *addr;
2163
2164 if (!bgp_configured)
2165 return CLI_OK;
2166
2167 if (CLI_HELP_REQUESTED)
2168 return cli_arg_help(cli, 1,
2169 "A.B.C.D", "BGP neighbour address",
2170 "NAME", "BGP neighbour name",
2171 NULL);
2172
2173 for (i = 0; i < BGP_NUM_PEERS; i++)
2174 {
2175 if (bgp_peers[i].state != Established)
2176 continue;
2177
2178 if (bgp_peers[i].routing)
2179 continue;
2180
2181 addr = inet_toa(bgp_peers[i].addr);
2182 if (argc && strcmp(addr, argv[0]) &&
2183 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2184 continue;
2185
2186 bgp_peers[i].cli_flag = BGP_CLI_ENABLE;
2187 cli_print(cli, "Un-suspending peer %s", bgp_peers[i].name);
2188 }
2189
2190 return CLI_OK;
2191 }
2192
2193 static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2194 {
2195 int i;
2196 char *addr;
2197
2198 if (!bgp_configured)
2199 return CLI_OK;
2200
2201 if (CLI_HELP_REQUESTED)
2202 return cli_arg_help(cli, 1,
2203 "A.B.C.D", "BGP neighbour address",
2204 "NAME", "BGP neighbour name",
2205 NULL);
2206
2207 for (i = 0; i < BGP_NUM_PEERS; i++)
2208 {
2209 if (!*bgp_peers[i].name)
2210 continue;
2211
2212 addr = inet_toa(bgp_peers[i].addr);
2213 if (argc && strcmp(addr, argv[0]) &&
2214 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2215 continue;
2216
2217 bgp_peers[i].cli_flag = BGP_CLI_RESTART;
2218 cli_print(cli, "Restarting peer %s", bgp_peers[i].name);
2219 }
2220
2221 return CLI_OK;
2222 }
2223 #endif /* BGP*/
2224
2225 // Convert a string in the form of abcd.ef12.3456 into char[6]
2226 void parsemac(char *string, char mac[6])
2227 {
2228 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)
2229 return;
2230 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)
2231 return;
2232 memset(mac, 0, 6);
2233 }