1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.24 2004/11/05 04:55:26 bodea Exp $";
17 #include <arpa/inet.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
34 extern tunnelt
*tunnel
;
35 extern sessiont
*session
;
36 extern radiust
*radius
;
37 extern ippoolt
*ip_address_pool
;
38 extern struct Tstats
*_statistics
;
39 struct cli_def
*cli
= NULL
;
41 extern struct configt
*config
;
42 extern struct config_descriptt config_values
[];
44 extern struct Tringbuffer
*ringbuffer
;
46 extern struct cli_session_actions
*cli_session_actions
;
47 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
48 extern tbft
*filter_list
;
50 char *debug_levels
[] = {
74 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
75 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 int cmd_show_cluster(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 int regular_stuff(struct cli_def
*cli
);
102 void parsemac(char *string
, char mac
[6]);
104 void init_cli(char *hostname
)
108 struct cli_command
*c
;
109 struct cli_command
*c2
;
111 struct sockaddr_in addr
;
114 if (hostname
&& *hostname
)
115 cli_set_hostname(cli
, hostname
);
117 cli_set_hostname(cli
, "l2tpns");
119 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
120 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
122 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
124 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
125 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
126 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
127 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
128 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
129 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
130 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
131 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
132 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
133 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
134 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");
135 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
137 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
138 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
139 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
142 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
144 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
145 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
148 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
150 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
151 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
152 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
154 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable interception for a user");
155 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable throttling for a user");
156 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
158 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
159 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP peer");
161 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
162 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable interception for a user");
163 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable throttling for a user");
164 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
165 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
166 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP peer");
168 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
169 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
170 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
171 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
173 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
174 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
176 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
177 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
179 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
180 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
182 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
184 // Enable regular processing
185 cli_regular(cli
, regular_stuff
);
187 if (!(f
= fopen(CLIUSERS
, "r")))
189 LOG(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
193 while (fgets(buf
, 4096, f
))
196 if (*buf
== '#') continue;
197 if ((p
= strchr(buf
, '\r'))) *p
= 0;
198 if ((p
= strchr(buf
, '\n'))) *p
= 0;
200 if (!(p
= strchr((char *)buf
, ':'))) continue;
202 if (!strcmp(buf
, "enable"))
204 cli_allow_enable(cli
, p
);
205 LOG(3, 0, 0, 0, "Setting enable password\n");
209 cli_allow_user(cli
, buf
, p
);
210 LOG(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
216 memset(&addr
, 0, sizeof(addr
));
217 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
218 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
221 // Set cli fd as non-blocking
222 flags
= fcntl(clifd
, F_GETFL
, 0);
223 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
225 addr
.sin_family
= AF_INET
;
226 addr
.sin_port
= htons(23);
227 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
229 LOG(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
235 void cli_do(int sockfd
)
237 int require_auth
= 1;
238 struct sockaddr_in addr
;
239 int l
= sizeof(addr
);
241 if (fork_and_close()) return;
242 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
244 LOG(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
245 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
248 LOG(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
252 LOG(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
253 if (!cli
->users
) /* paranoia */
255 LOG(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
261 /* no username/pass required */
268 debug_rb_tail
= ringbuffer
->tail
;
270 memset(&debug_flags
, 0, sizeof(debug_flags
));
271 debug_flags
.critical
= 1;
273 cli_loop(cli
, sockfd
);
276 LOG(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
280 void cli_print_log(struct cli_def
*cli
, char *string
)
282 LOG(3, 0, 0, 0, "%s\n", string
);
285 void cli_do_file(FILE *fh
)
287 LOG(3, 0, 0, 0, "Reading configuration file\n");
288 cli_print_callback(cli
, cli_print_log
);
289 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
290 cli_print_callback(cli
, NULL
);
293 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
304 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
306 int v
= va_arg(ap
, int);
307 snprintf(buf
, sizeof(buf
), entry
, v
);
313 desc
= va_arg(ap
, char *);
315 cli_print(cli
, " %-20s %s", p
, desc
);
317 cli_print(cli
, " %s", p
);
319 entry
= desc
? va_arg(ap
, char *) : 0;
324 cli_print(cli
, " <cr>");
329 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
333 if (CLI_HELP_REQUESTED
)
334 return cli_arg_help(cli
, 1,
335 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
341 // Show individual session
342 for (i
= 0; i
< argc
; i
++)
344 unsigned int s
, b_in
, b_out
;
346 if (s
<= 0 || s
>= MAXSESSION
)
348 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
351 cli_print(cli
, "\r\nSession %d:", s
);
352 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
353 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
354 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
355 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
356 cli_print(cli
, "\tIP address:\t%s", inet_toa(htonl(session
[s
].ip
)));
357 cli_print(cli
, "\tUnique SID:\t%lu", session
[s
].unique_id
);
358 cli_print(cli
, "\tIdle time:\t%u seconds", abs(time_now
- session
[s
].last_packet
));
359 cli_print(cli
, "\tNext Recv:\t%u", session
[s
].nr
);
360 cli_print(cli
, "\tNext Send:\t%u", session
[s
].ns
);
361 cli_print(cli
, "\tBytes In/Out:\t%lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
362 cli_print(cli
, "\tPkts In/Out:\t%lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
363 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
364 cli_print(cli
, "\tRadius Session:\t%u", session
[s
].radius
);
365 cli_print(cli
, "\tRx Speed:\t%lu", session
[s
].rx_connect_speed
);
366 cli_print(cli
, "\tTx Speed:\t%lu", session
[s
].tx_connect_speed
);
367 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
368 cli_print(cli
, "\tIntercepted:\t%s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
370 cli_print(cli
, "\tIntercepted:\tno");
372 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
374 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
375 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
376 t
? "YES" : "no", t
? " (" : "",
377 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
379 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
383 b_in
= session
[s
].tbf_in
;
384 b_out
= session
[s
].tbf_out
;
386 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
387 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
388 "ByteSent", "PackSent", "PackDrop", "PackDelay");
391 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
393 (filter_list
[b_in
].next
? "*" : " "),
394 (b_in
< 100 ? "\t" : ""),
395 filter_list
[b_in
].rate
* 8,
396 filter_list
[b_in
].credit
,
397 filter_list
[b_in
].queued
,
398 filter_list
[b_in
].b_queued
,
399 filter_list
[b_in
].p_queued
,
400 filter_list
[b_in
].b_sent
,
401 filter_list
[b_in
].p_sent
,
402 filter_list
[b_in
].p_dropped
,
403 filter_list
[b_in
].p_delayed
);
406 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
408 (filter_list
[b_out
].next
? "*" : " "),
409 (b_out
< 100 ? "\t" : ""),
410 filter_list
[b_out
].rate
* 8,
411 filter_list
[b_out
].credit
,
412 filter_list
[b_out
].queued
,
413 filter_list
[b_out
].b_queued
,
414 filter_list
[b_out
].p_queued
,
415 filter_list
[b_out
].b_sent
,
416 filter_list
[b_out
].p_sent
,
417 filter_list
[b_out
].p_dropped
,
418 filter_list
[b_out
].p_delayed
);
425 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
440 for (i
= 1; i
< MAXSESSION
; i
++)
442 char *userip
, *tunnelip
;
443 if (!session
[i
].opened
) continue;
444 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
445 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
446 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
449 session
[i
].user
[0] ? session
[i
].user
: "*",
451 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
452 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
453 (session
[i
].walled_garden
) ? "Y" : "N",
454 abs(time_now
- (unsigned long)session
[i
].opened
),
455 (unsigned long)session
[i
].total_cout
,
456 (unsigned long)session
[i
].total_cin
,
457 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
459 session
[i
].calling
[0] ? session
[i
].calling
: "*");
460 if (userip
) free(userip
);
461 if (tunnelip
) free(tunnelip
);
466 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
468 int i
, x
, show_all
= 0;
476 if (CLI_HELP_REQUESTED
)
479 return cli_arg_help(cli
, 1,
480 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
483 return cli_arg_help(cli
, 1,
484 "all", "Show all tunnels, including unused",
485 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
492 if (strcmp(argv
[0], "all") == 0)
498 // Show individual tunnel
499 for (i
= 0; i
< argc
; i
++)
504 if (t
<= 0 || t
>= MAXTUNNEL
)
506 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
509 cli_print(cli
, "\r\nTunnel %d:", t
);
510 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
511 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
512 cli_print(cli
, "\tRemote IP:\t%s", inet_toa(htonl(tunnel
[t
].ip
)));
513 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
514 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
515 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
516 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
517 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
518 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
520 for (x
= 0; x
< MAXSESSION
; x
++)
521 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
522 sprintf(s
, "%s%u ", s
, x
);
524 cli_print(cli
, "\tSessions:\t%s", s
);
530 // Show tunnel summary
531 cli_print(cli
, "%4s %20s %20s %6s %s",
538 for (i
= 1; i
< MAXTUNNEL
; i
++)
541 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
543 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
544 cli_print(cli
, "%4d %20s %20s %6s %6d",
546 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
547 inet_toa(htonl(tunnel
[i
].ip
)),
548 states
[tunnel
[i
].state
],
555 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
562 if (CLI_HELP_REQUESTED
)
563 return cli_arg_help(cli
, 1,
564 "USER", "Show details for specific username",
567 for (i
= 0; i
< MAXSESSION
; i
++)
569 if (!session
[i
].opened
) continue;
570 if (!session
[i
].user
[0]) continue;
574 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
576 if (strcmp(argv
[j
], session
[i
].user
) == 0)
578 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
579 sargv
[sargc
] = sid
[sargc
];
587 cli_print(cli
, "%s", session
[i
].user
);
591 return cmd_show_session(cli
, "users", sargv
, sargc
);
596 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
598 if (CLI_HELP_REQUESTED
)
599 return CLI_HELP_NO_ARGS
;
601 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
602 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
603 GET_STAT(tun_rx_bytes
),
604 GET_STAT(tun_rx_packets
),
605 GET_STAT(tun_rx_errors
));
606 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
607 GET_STAT(tun_tx_bytes
),
608 GET_STAT(tun_tx_packets
),
609 GET_STAT(tun_tx_errors
));
612 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
613 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
614 GET_STAT(tunnel_rx_bytes
),
615 GET_STAT(tunnel_rx_packets
),
616 GET_STAT(tunnel_rx_errors
),
618 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
619 GET_STAT(tunnel_tx_bytes
),
620 GET_STAT(tunnel_tx_packets
),
621 GET_STAT(tunnel_tx_errors
),
622 GET_STAT(tunnel_retries
));
625 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
626 cli_print(cli
, "-----------------------------------------");
627 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
628 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
629 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
630 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
631 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
632 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
633 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
634 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
635 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
636 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
637 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
638 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
639 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
640 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
641 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
645 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
646 cli_print(cli
, "-----------------------------------------");
647 cli_print(cli
, "%-30s%lu", "call_processtun", GET_STAT(call_processtun
));
648 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
649 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
650 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
651 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
652 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
653 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
654 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
655 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
656 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
657 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
658 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
659 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
660 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
661 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
662 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
663 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
664 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
665 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
666 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
667 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
668 cli_print(cli
, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
669 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
670 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
671 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
672 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
677 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
683 if (CLI_HELP_REQUESTED
)
684 return cli_arg_help(cli
, 1,
685 "tag", "Include CVS release tag",
686 "file", "Include file versions",
689 for (i
= 0; i
< argc
; i
++)
690 if (!strcmp(argv
[i
], "tag"))
692 else if (!strcmp(argv
[i
], "file"))
695 cli_print(cli
, "L2TPNS %s", VERSION
);
698 char const *p
= strchr(cvs_name
, ':');
710 e
= strpbrk(p
, " \t$");
711 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
716 extern linked_list
*loaded_plugins
;
719 cli_print(cli
, "Files:");
720 cli_print(cli
, " %s", cvs_id_arp
);
722 cli_print(cli
, " %s", cvs_id_bgp
);
724 cli_print(cli
, " %s", cvs_id_cli
);
725 cli_print(cli
, " %s", cvs_id_cluster
);
726 cli_print(cli
, " %s", cvs_id_constants
);
727 cli_print(cli
, " %s", cvs_id_control
);
728 cli_print(cli
, " %s", cvs_id_icmp
);
729 cli_print(cli
, " %s", cvs_id_l2tpns
);
730 cli_print(cli
, " %s", cvs_id_ll
);
731 cli_print(cli
, " %s", cvs_id_md5
);
732 cli_print(cli
, " %s", cvs_id_ppp
);
733 cli_print(cli
, " %s", cvs_id_radius
);
734 cli_print(cli
, " %s", cvs_id_tbf
);
735 cli_print(cli
, " %s", cvs_id_util
);
737 ll_reset(loaded_plugins
);
738 while ((p
= ll_next(loaded_plugins
)))
740 char const **id
= dlsym(p
, "cvs_id");
742 cli_print(cli
, " %s", *id
);
749 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
752 int used
= 0, free
= 0, show_all
= 0;
754 if (!config
->cluster_iam_master
)
756 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
760 if (CLI_HELP_REQUESTED
)
763 return cli_arg_help(cli
, 1, NULL
);
765 return cli_arg_help(cli
, 1,
766 "all", "Show all pool addresses, including unused",
770 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
774 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
775 for (i
= 0; i
< MAXIPPOOL
; i
++)
777 if (!ip_address_pool
[i
].address
) continue;
778 if (ip_address_pool
[i
].assigned
)
780 cli_print(cli
, "%-15s\tY %8d %s",
781 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
787 if (ip_address_pool
[i
].last
)
788 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
789 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
790 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
792 cli_print(cli
, "%-15s\tN", inet_toa(htonl(ip_address_pool
[i
].address
)));
799 cli_print(cli
, "(Not displaying unused addresses)");
801 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
805 void print_save_config(struct cli_def
*cli
, char *string
)
808 fprintf(save_config_fh
, "%s\n", string
);
811 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
813 if (CLI_HELP_REQUESTED
)
814 return CLI_HELP_NO_ARGS
;
816 if ((save_config_fh
= fopen(config
->config_file
, "w")))
818 cli_print(cli
, "Writing configuration");
819 cli_print_callback(cli
, print_save_config
);
820 cmd_show_run(cli
, command
, argv
, argc
);
821 cli_print_callback(cli
, NULL
);
822 fclose(save_config_fh
);
826 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
831 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
835 if (CLI_HELP_REQUESTED
)
836 return CLI_HELP_NO_ARGS
;
838 cli_print(cli
, "# Current configuration:");
840 for (i
= 0; config_values
[i
].key
; i
++)
842 void *value
= ((void *)config
) + config_values
[i
].offset
;
843 if (config_values
[i
].type
== STRING
)
844 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
845 else if (config_values
[i
].type
== IP
)
846 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
847 else if (config_values
[i
].type
== SHORT
)
848 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
849 else if (config_values
[i
].type
== BOOL
)
850 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
851 else if (config_values
[i
].type
== INT
)
852 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
853 else if (config_values
[i
].type
== UNSIGNED_LONG
)
854 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
855 else if (config_values
[i
].type
== MAC
)
856 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
857 *(unsigned short *)(value
+ 0),
858 *(unsigned short *)(value
+ 1),
859 *(unsigned short *)(value
+ 2),
860 *(unsigned short *)(value
+ 3),
861 *(unsigned short *)(value
+ 4),
862 *(unsigned short *)(value
+ 5));
865 cli_print(cli
, "# Plugins");
866 for (i
= 0; i
< MAXPLUGINS
; i
++)
868 if (*config
->plugins
[i
])
870 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
874 cli_print(cli
, "# end");
878 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
880 int i
, free
= 0, used
= 0, show_all
= 0;
891 if (CLI_HELP_REQUESTED
)
894 return cli_arg_help(cli
, 1, NULL
);
896 return cli_arg_help(cli
, 1,
897 "all", "Show all RADIUS sessions, including unused",
901 cli_print(cli
, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
905 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
908 for (i
= 1; i
< MAXRADIUS
; i
++)
910 if (radius
[i
].state
== RADIUSNULL
)
915 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
917 cli_print(cli
, "%6d%6d%5d%6s%9d%9u%4d",
921 states
[radius
[i
].state
],
927 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
932 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
936 if (CLI_HELP_REQUESTED
)
937 return CLI_HELP_NO_ARGS
;
939 cli_print(cli
, "Plugins currently loaded:");
940 for (i
= 0; i
< MAXPLUGINS
; i
++)
941 if (*config
->plugins
[i
])
942 cli_print(cli
, " %s", config
->plugins
[i
]);
947 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
951 if (CLI_HELP_REQUESTED
)
952 return CLI_HELP_NO_ARGS
;
954 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
963 for (i
= 0; i
< MAXSESSION
; i
++)
965 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
966 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
970 session
[i
].throttle_in
,
971 session
[i
].throttle_out
,
979 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
981 if (CLI_HELP_REQUESTED
)
982 return CLI_HELP_NO_ARGS
;
984 cli_print(cli
, " _\n"
990 " ( \\ `. `-. _,.-:\\\n"
991 " \\ \\ `. `-._ __..--' ,-';/\n"
992 " \\ `. `-. `-..___..---' _.--' ,'/\n"
993 " `. `. `-._ __..--' ,' /\n"
994 " `. `-_ ``--..'' _.-' ,'\n"
995 " `-_ `-.___ __,--' ,'\n"
996 " `-.__ `----\"\"\" __.-'\n"
997 "hh `--..____..--'");
1002 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1004 if (CLI_HELP_REQUESTED
)
1005 return CLI_HELP_NO_ARGS
;
1007 cli_print(cli
, "Counters cleared");
1008 SET_STAT(last_reset
, time(NULL
));
1012 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1017 if (CLI_HELP_REQUESTED
)
1018 return cli_arg_help(cli
, argc
> 1,
1019 "USER", "Username of session to drop", NULL
);
1021 if (!config
->cluster_iam_master
)
1023 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1029 cli_print(cli
, "Specify a user to drop");
1033 for (i
= 0; i
< argc
; i
++)
1035 if (!(s
= sessionbyuser(argv
[i
])))
1037 cli_print(cli
, "User %s is not connected", argv
[i
]);
1041 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1043 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1044 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1051 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1056 if (CLI_HELP_REQUESTED
)
1057 return cli_arg_help(cli
, argc
> 1,
1058 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1060 if (!config
->cluster_iam_master
)
1062 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1068 cli_print(cli
, "Specify a tunnel to drop");
1072 for (i
= 0; i
< argc
; i
++)
1074 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1076 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1082 cli_print(cli
, "Tunnel %d is not connected", t
);
1088 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1092 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1093 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1099 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1104 if (CLI_HELP_REQUESTED
)
1105 return cli_arg_help(cli
, argc
> 1,
1106 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1108 if (!config
->cluster_iam_master
)
1110 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1116 cli_print(cli
, "Specify a session id to drop");
1120 for (i
= 0; i
< argc
; i
++)
1122 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1124 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1128 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1130 cli_print(cli
, "Dropping session %d", s
);
1131 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1135 cli_print(cli
, "Session %d is not active.", s
);
1142 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1148 if (CLI_HELP_REQUESTED
)
1153 return cli_arg_help(cli
, 0,
1154 "USER", "Username of session to snoop", NULL
);
1157 return cli_arg_help(cli
, 0,
1158 "A.B.C.D", "IP address of snoop destination", NULL
);
1161 return cli_arg_help(cli
, 0,
1162 "N", "Port of snoop destination", NULL
);
1166 return cli_arg_help(cli
, 1, NULL
);
1173 if (!config
->cluster_iam_master
)
1175 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1181 cli_print(cli
, "Specify username, ip and port");
1185 if (!(s
= sessionbyuser(argv
[0])))
1187 cli_print(cli
, "User %s is not connected", argv
[0]);
1191 ip
= inet_addr(argv
[1]);
1192 if (!ip
|| ip
== INADDR_NONE
)
1194 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1198 port
= atoi(argv
[2]);
1201 cli_print(cli
, "Invalid port %s", argv
[2]);
1205 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(ip
), port
);
1206 cli_session_actions
[s
].snoop_ip
= ip
;
1207 cli_session_actions
[s
].snoop_port
= port
;
1208 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1213 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1218 if (CLI_HELP_REQUESTED
)
1219 return cli_arg_help(cli
, argc
> 1,
1220 "USER", "Username of session to unsnoop", NULL
);
1222 if (!config
->cluster_iam_master
)
1224 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1230 cli_print(cli
, "Specify a user to unsnoop");
1234 for (i
= 0; i
< argc
; i
++)
1236 if (!(s
= sessionbyuser(argv
[i
])))
1238 cli_print(cli
, "User %s is not connected", argv
[i
]);
1242 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1243 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1249 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1256 throttle USER - throttle in/out to default rate
1257 throttle USER RATE - throttle in/out to default rate
1258 throttle USER in RATE - throttle input only
1259 throttle USER out RATE - throttle output only
1260 throttle USER in RATE out RATE - throttle both
1263 if (CLI_HELP_REQUESTED
)
1268 return cli_arg_help(cli
, 0,
1269 "USER", "Username of session to throttle", NULL
);
1272 return cli_arg_help(cli
, 1,
1273 "RATE", "Rate in kbps (in and out)",
1274 "in", "Select incoming rate",
1275 "out", "Select outgoing rate", NULL
);
1278 return cli_arg_help(cli
, 1,
1279 "in", "Select incoming rate",
1280 "out", "Select outgoing rate", NULL
);
1283 if (isdigit(argv
[1][0]))
1284 return cli_arg_help(cli
, 1, NULL
);
1287 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1290 return cli_arg_help(cli
, argc
> 1, NULL
);
1294 if (!config
->cluster_iam_master
)
1296 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1302 cli_print(cli
, "Specify a user to throttle");
1306 if (!(s
= sessionbyuser(argv
[0])))
1308 cli_print(cli
, "User %s is not connected", argv
[0]);
1314 rate_in
= rate_out
= config
->rl_rate
;
1318 rate_in
= rate_out
= atoi(argv
[1]);
1321 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1325 else if (argc
== 3 || argc
== 5)
1328 for (i
= 1; i
< argc
- 1; i
+= 2)
1330 int len
= strlen(argv
[i
]);
1332 if (!strncasecmp(argv
[i
], "in", len
))
1333 r
= rate_in
= atoi(argv
[i
+1]);
1334 else if (!strncasecmp(argv
[i
], "out", len
))
1335 r
= rate_out
= atoi(argv
[i
+1]);
1339 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1346 cli_print(cli
, "Invalid arguments");
1350 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1352 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1356 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1357 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1358 cli_session_actions
[s
].throttle_in
= rate_in
;
1360 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1361 cli_session_actions
[s
].throttle_out
= rate_out
;
1363 if (cli_session_actions
[s
].throttle_in
== -1 &&
1364 cli_session_actions
[s
].throttle_out
== -1)
1366 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1370 cli_print(cli
, "Throttling user %s", argv
[0]);
1371 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1376 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1381 if (CLI_HELP_REQUESTED
)
1382 return cli_arg_help(cli
, argc
> 1,
1383 "USER", "Username of session to unthrottle", NULL
);
1385 if (!config
->cluster_iam_master
)
1387 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1393 cli_print(cli
, "Specify a user to unthrottle");
1397 for (i
= 0; i
< argc
; i
++)
1399 if (!(s
= sessionbyuser(argv
[i
])))
1401 cli_print(cli
, "User %s is not connected", argv
[i
]);
1405 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1407 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1408 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1412 cli_print(cli
, "User %s not throttled", argv
[i
]);
1419 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1423 if (CLI_HELP_REQUESTED
)
1424 return cli_arg_help(cli
, 1,
1425 "all", "Enable debugging for all except \"data\"",
1426 "critical", "", // FIXME: add descriptions
1436 char *p
= (char *) &debug_flags
;
1437 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1441 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1442 (debug_flags
.critical
) ? " critical" : "",
1443 (debug_flags
.error
) ? " error" : "",
1444 (debug_flags
.warning
) ? " warning" : "",
1445 (debug_flags
.info
) ? " info" : "",
1446 (debug_flags
.calls
) ? " calls" : "",
1447 (debug_flags
.data
) ? " data" : "");
1453 cli_print(cli
, "Debugging off");
1457 for (i
= 0; i
< argc
; i
++)
1459 int len
= strlen(argv
[i
]);
1461 if (argv
[i
][0] == 'c' && len
< 2)
1462 len
= 2; /* distinguish [cr]itical from [ca]lls */
1464 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 1; continue; }
1465 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 1; continue; }
1466 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 1; continue; }
1467 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 1; continue; }
1468 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 1; continue; }
1469 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 1; continue; }
1470 if (!strncasecmp(argv
[i
], "all", len
))
1472 memset(&debug_flags
, 1, sizeof(debug_flags
));
1473 debug_flags
.data
= 0;
1477 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1483 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1487 if (CLI_HELP_REQUESTED
)
1488 return cli_arg_help(cli
, 1,
1489 "all", "Disable all debugging",
1490 "critical", "", // FIXME: add descriptions
1500 memset(&debug_flags
, 0, sizeof(debug_flags
));
1504 for (i
= 0; i
< argc
; i
++)
1506 int len
= strlen(argv
[i
]);
1508 if (argv
[i
][0] == 'c' && len
< 2)
1509 len
= 2; /* distinguish [cr]itical from [ca]lls */
1511 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 0; continue; }
1512 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 0; continue; }
1513 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 0; continue; }
1514 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 0; continue; }
1515 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 0; continue; }
1516 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 0; continue; }
1517 if (!strncasecmp(argv
[i
], "all", len
))
1519 memset(&debug_flags
, 0, sizeof(debug_flags
));
1523 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1529 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1531 int i
, firstfree
= 0;
1533 if (CLI_HELP_REQUESTED
)
1534 return cli_arg_help(cli
, argc
> 1,
1535 "PLUGIN", "Name of plugin to load", NULL
);
1539 cli_print(cli
, "Specify a plugin to load");
1543 for (i
= 0; i
< MAXPLUGINS
; i
++)
1545 if (!*config
->plugins
[i
] && !firstfree
)
1547 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1549 cli_print(cli
, "Plugin is already loaded");
1556 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1557 config
->reload_config
= 1;
1558 cli_print(cli
, "Loading plugin %s", argv
[0]);
1564 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1568 if (CLI_HELP_REQUESTED
)
1569 return cli_arg_help(cli
, argc
> 1,
1570 "PLUGIN", "Name of plugin to unload", NULL
);
1574 cli_print(cli
, "Specify a plugin to remove");
1578 for (i
= 0; i
< MAXPLUGINS
; i
++)
1580 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1582 config
->reload_config
= 1;
1583 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1588 cli_print(cli
, "Plugin is not loaded");
1592 char *duration(time_t secs
)
1594 static char *buf
= NULL
;
1597 if (!buf
) buf
= calloc(64, 1);
1601 int days
= secs
/ 86400;
1602 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1608 int mins
= secs
/ 60;
1609 int hrs
= mins
/ 60;
1612 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1614 else if (secs
>= 60)
1616 int mins
= secs
/ 60;
1617 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1620 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1625 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1628 char buf
[100], *p
= buf
, *loads
[3];
1629 int i
, num_sessions
= 0;
1631 if (CLI_HELP_REQUESTED
)
1632 return CLI_HELP_NO_ARGS
;
1634 fh
= fopen("/proc/loadavg", "r");
1635 fgets(buf
, 100, fh
);
1638 for (i
= 0; i
< 3; i
++)
1639 loads
[i
] = strdup(strsep(&p
, " "));
1642 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1644 for (i
= 1; i
< MAXSESSION
; i
++)
1645 if (session
[i
].opened
) num_sessions
++;
1647 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1649 duration(time_now
- config
->start_time
),
1651 loads
[0], loads
[1], loads
[2]
1653 for (i
= 0; i
< 3; i
++)
1654 if (loads
[i
]) free(loads
[i
]);
1656 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1661 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1665 if (CLI_HELP_REQUESTED
)
1671 int len
= strlen(argv
[0])-1;
1672 for (i
= 0; config_values
[i
].key
; i
++)
1673 if (!len
|| !strncmp(argv
[0], config_values
[i
].key
, len
))
1674 cli_print(cli
, " %s", config_values
[i
].key
);
1680 return cli_arg_help(cli
, 0,
1681 "VALUE", "Value for variable", NULL
);
1685 return cli_arg_help(cli
, 1, NULL
);
1694 cli_print(cli
, "Specify variable and value");
1698 for (i
= 0; config_values
[i
].key
; i
++)
1700 void *value
= ((void *)config
) + config_values
[i
].offset
;
1701 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1703 // Found a value to set
1704 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1705 switch (config_values
[i
].type
)
1708 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1711 *(int *)value
= atoi(argv
[1]);
1714 *(unsigned long *)value
= atol(argv
[1]);
1717 *(short *)value
= atoi(argv
[1]);
1720 *(unsigned *)value
= inet_addr(argv
[1]);
1723 parsemac(argv
[1], (char *)value
);
1726 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1732 cli_print(cli
, "Unknown variable type");
1735 config
->reload_config
= 1;
1740 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1744 int regular_stuff(struct cli_def
*cli
)
1746 int i
= debug_rb_tail
;
1750 while (i
!= ringbuffer
->tail
)
1752 int show_message
= 0;
1754 if (*ringbuffer
->buffer
[i
].message
)
1756 // Always show messages if we are doing general debug
1757 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1758 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1759 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1760 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1761 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1762 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1767 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1769 struct in_addr addr
;
1771 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1772 ipaddr
= inet_ntoa(addr
);
1774 cli_print(cli
, "\r%s-%s-%u-%u %s",
1775 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1777 ringbuffer
->buffer
[i
].tunnel
,
1778 ringbuffer
->buffer
[i
].session
,
1779 ringbuffer
->buffer
[i
].message
);
1784 if (++i
== ringbuffer
->tail
) break;
1785 if (i
== RINGBUFFER_SIZE
) i
= 0;
1788 debug_rb_tail
= ringbuffer
->tail
;
1795 // Convert a string in the form of abcd.ef12.3456 into char[6]
1796 void parsemac(char *string
, char mac
[6])
1798 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)
1800 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)