1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.70 2005/12/06 00:00:31 bodea Exp $";
19 #include <arpa/inet.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
29 #include "constants.h"
38 extern tunnelt
*tunnel
;
39 extern sessiont
*session
;
40 extern radiust
*radius
;
41 extern ippoolt
*ip_address_pool
;
42 extern struct Tstats
*_statistics
;
43 static struct cli_def
*cli
= NULL
;
44 extern configt
*config
;
45 extern config_descriptt config_values
[];
47 extern struct Tringbuffer
*ringbuffer
;
49 extern struct cli_session_actions
*cli_session_actions
;
50 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
51 extern tbft
*filter_list
;
52 extern ip_filtert
*ip_filters
;
66 static int debug_rb_tail
;
67 static char *debug_levels
[] = {
78 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
103 static int regular_stuff(struct cli_def
*cli
);
104 static void parsemac(char *string
, char mac
[6]);
107 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 #endif /* STATISTICS */
112 #define MODE_CONFIG_BGP 8
113 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
114 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
115 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
116 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
122 #define MODE_CONFIG_NACL 9
123 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
124 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
125 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
126 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
127 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
128 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
130 /* match if b is a substr of a */
131 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
133 void init_cli(char *hostname
)
137 struct cli_command
*c
;
138 struct cli_command
*c2
;
140 struct sockaddr_in addr
;
143 if (hostname
&& *hostname
)
144 cli_set_hostname(cli
, hostname
);
146 cli_set_hostname(cli
, "l2tpns");
148 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
149 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
151 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
153 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
154 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
155 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
156 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
157 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
158 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
159 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
160 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
161 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
162 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
163 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");
164 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
165 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
167 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
168 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
169 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
172 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
174 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
175 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
178 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
180 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
181 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
182 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
184 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
185 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
186 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
187 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
190 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
191 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
194 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
195 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
196 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
197 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
198 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
201 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
202 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
204 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
205 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
207 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
208 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
210 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
212 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
213 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
216 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
217 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
218 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
219 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
221 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
222 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
224 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
225 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
227 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
229 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
230 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
232 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
233 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
235 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
236 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
237 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
239 // Enable regular processing
240 cli_regular(cli
, regular_stuff
);
242 if (!(f
= fopen(CLIUSERS
, "r")))
244 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
248 while (fgets(buf
, 4096, f
))
251 if (*buf
== '#') continue;
252 if ((p
= strchr(buf
, '\r'))) *p
= 0;
253 if ((p
= strchr(buf
, '\n'))) *p
= 0;
255 if (!(p
= strchr((char *)buf
, ':'))) continue;
257 if (!strcmp(buf
, "enable"))
259 cli_allow_enable(cli
, p
);
260 LOG(3, 0, 0, "Setting enable password\n");
264 cli_allow_user(cli
, buf
, p
);
265 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
271 memset(&addr
, 0, sizeof(addr
));
272 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
273 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
276 // Set cli fd as non-blocking
277 flags
= fcntl(clifd
, F_GETFL
, 0);
278 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
280 addr
.sin_family
= AF_INET
;
281 addr
.sin_port
= htons(23);
282 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
284 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
290 void cli_do(int sockfd
)
292 int require_auth
= 1;
293 struct sockaddr_in addr
;
294 socklen_t l
= sizeof(addr
);
296 if (fork_and_close()) return;
297 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
299 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
300 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
301 fmtaddr(addr
.sin_addr
.s_addr
, 0));
304 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
308 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
309 if (!cli
->users
) /* paranoia */
311 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
317 /* no username/pass required */
322 debug_rb_tail
= ringbuffer
->tail
;
324 memset(&debug_flags
, 0, sizeof(debug_flags
));
325 debug_flags
.critical
= 1;
327 cli_loop(cli
, sockfd
);
330 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
331 fmtaddr(addr
.sin_addr
.s_addr
, 0));
336 static void cli_print_log(struct cli_def
*cli
, char *string
)
338 LOG(3, 0, 0, "%s\n", string
);
341 void cli_do_file(FILE *fh
)
343 LOG(3, 0, 0, "Reading configuration file\n");
344 cli_print_callback(cli
, cli_print_log
);
345 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
346 cli_print_callback(cli
, NULL
);
349 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
360 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
362 int v
= va_arg(ap
, int);
363 snprintf(buf
, sizeof(buf
), entry
, v
);
369 desc
= va_arg(ap
, char *);
371 cli_error(cli
, " %-20s %s", p
, desc
);
373 cli_error(cli
, " %s", p
);
375 entry
= desc
? va_arg(ap
, char *) : 0;
380 cli_error(cli
, " <cr>");
385 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
389 if (CLI_HELP_REQUESTED
)
390 return cli_arg_help(cli
, 1,
391 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
397 // Show individual session
398 for (i
= 0; i
< argc
; i
++)
400 unsigned int s
, b_in
, b_out
;
402 if (s
<= 0 || s
>= MAXSESSION
)
404 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
407 cli_print(cli
, "\r\nSession %d:", s
);
408 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
409 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
410 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
411 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
412 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
413 switch (session
[s
].ppp
.phase
)
416 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
421 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
422 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
423 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
425 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
426 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
427 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
428 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
429 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
430 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
431 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
432 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
433 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
434 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
435 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
436 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
437 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
438 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
439 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
441 cli_print(cli
, "\tIntercepted:\tno");
443 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
445 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
446 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
447 t
? "YES" : "no", t
? " (" : "",
448 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
450 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
454 b_in
= session
[s
].tbf_in
;
455 b_out
= session
[s
].tbf_out
;
457 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
458 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
459 "ByteSent", "PackSent", "PackDrop", "PackDelay");
462 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
464 (filter_list
[b_in
].next
? "*" : " "),
465 (b_in
< 100 ? "\t" : ""),
466 filter_list
[b_in
].rate
* 8,
467 filter_list
[b_in
].credit
,
468 filter_list
[b_in
].queued
,
469 filter_list
[b_in
].b_queued
,
470 filter_list
[b_in
].p_queued
,
471 filter_list
[b_in
].b_sent
,
472 filter_list
[b_in
].p_sent
,
473 filter_list
[b_in
].p_dropped
,
474 filter_list
[b_in
].p_delayed
);
477 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
479 (filter_list
[b_out
].next
? "*" : " "),
480 (b_out
< 100 ? "\t" : ""),
481 filter_list
[b_out
].rate
* 8,
482 filter_list
[b_out
].credit
,
483 filter_list
[b_out
].queued
,
484 filter_list
[b_out
].b_queued
,
485 filter_list
[b_out
].p_queued
,
486 filter_list
[b_out
].b_sent
,
487 filter_list
[b_out
].p_sent
,
488 filter_list
[b_out
].p_dropped
,
489 filter_list
[b_out
].p_delayed
);
496 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %-15s %s",
512 for (i
= 1; i
< MAXSESSION
; i
++)
514 if (!session
[i
].opened
) continue;
515 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %-15s %s",
518 session
[i
].user
[0] ? session
[i
].user
: "*",
519 fmtaddr(htonl(session
[i
].ip
), 0),
520 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
521 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
522 (session
[i
].walled_garden
) ? "Y" : "N",
523 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
524 abs(time_now
- (unsigned long)session
[i
].opened
),
525 (unsigned long)session
[i
].cout
,
526 (unsigned long)session
[i
].cin
,
527 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
528 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
529 session
[i
].calling
[0] ? session
[i
].calling
: "*");
534 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
536 int i
, x
, show_all
= 0;
544 if (CLI_HELP_REQUESTED
)
547 return cli_arg_help(cli
, 1,
548 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
551 return cli_arg_help(cli
, 1,
552 "all", "Show all tunnels, including unused",
553 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
560 if (strcmp(argv
[0], "all") == 0)
566 // Show individual tunnel
567 for (i
= 0; i
< argc
; i
++)
572 if (t
<= 0 || t
>= MAXTUNNEL
)
574 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
577 cli_print(cli
, "\r\nTunnel %d:", t
);
578 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
579 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
580 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
581 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
582 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
583 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
584 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
585 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
586 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
588 for (x
= 0; x
< MAXSESSION
; x
++)
589 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
590 sprintf(s
, "%s%u ", s
, x
);
592 cli_print(cli
, "\tSessions:\t%s", s
);
598 // Show tunnel summary
599 cli_print(cli
, "%4s %20s %20s %6s %s",
606 for (i
= 1; i
< MAXTUNNEL
; i
++)
609 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
611 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
612 cli_print(cli
, "%4d %20s %20s %6s %6d",
614 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
615 fmtaddr(htonl(tunnel
[i
].ip
), 0),
616 states
[tunnel
[i
].state
],
623 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
630 if (CLI_HELP_REQUESTED
)
631 return cli_arg_help(cli
, 1,
632 "USER", "Show details for specific username",
635 for (i
= 0; i
< MAXSESSION
; i
++)
637 if (!session
[i
].opened
) continue;
638 if (!session
[i
].user
[0]) continue;
642 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
644 if (strcmp(argv
[j
], session
[i
].user
) == 0)
646 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
647 sargv
[sargc
] = sid
[sargc
];
655 cli_print(cli
, "%s", session
[i
].user
);
659 return cmd_show_session(cli
, "users", sargv
, sargc
);
665 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
667 if (CLI_HELP_REQUESTED
)
668 return CLI_HELP_NO_ARGS
;
670 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
671 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
672 GET_STAT(tun_rx_bytes
),
673 GET_STAT(tun_rx_packets
),
674 GET_STAT(tun_rx_errors
),
675 GET_STAT(tun_rx_dropped
));
676 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
677 GET_STAT(tun_tx_bytes
),
678 GET_STAT(tun_tx_packets
),
679 GET_STAT(tun_tx_errors
));
682 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
683 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
684 GET_STAT(tunnel_rx_bytes
),
685 GET_STAT(tunnel_rx_packets
),
686 GET_STAT(tunnel_rx_errors
));
687 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
688 GET_STAT(tunnel_tx_bytes
),
689 GET_STAT(tunnel_tx_packets
),
690 GET_STAT(tunnel_tx_errors
),
691 GET_STAT(tunnel_retries
));
694 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
695 cli_print(cli
, "-----------------------------------------");
696 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
697 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
698 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
699 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
700 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
701 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
702 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
703 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
704 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
705 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
706 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
707 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
708 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
709 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
710 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
711 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
712 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
713 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
717 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
718 cli_print(cli
, "-----------------------------------------");
719 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
720 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
721 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
722 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
723 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
724 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
725 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
726 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
727 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
728 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
729 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
730 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
731 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
732 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
733 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
734 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
735 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
736 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
737 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
738 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
739 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
740 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
741 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
742 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
743 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
744 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
745 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
746 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
747 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
748 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
749 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
750 #endif /* STAT_CALLS */
753 time_t l
= GET_STAT(last_reset
);
755 char *p
= strchr(t
, '\n');
759 cli_print(cli
, "Last counter reset %s", t
);
765 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
767 if (CLI_HELP_REQUESTED
)
768 return CLI_HELP_NO_ARGS
;
770 memset(_statistics
, 0, sizeof(struct Tstats
));
771 SET_STAT(last_reset
, time(NULL
));
773 cli_print(cli
, "Counters cleared");
776 #endif /* STATISTICS */
778 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
784 if (CLI_HELP_REQUESTED
)
785 return cli_arg_help(cli
, 1,
786 "tag", "Include CVS release tag",
787 "file", "Include file versions",
790 for (i
= 0; i
< argc
; i
++)
791 if (!strcmp(argv
[i
], "tag"))
793 else if (!strcmp(argv
[i
], "file"))
796 cli_print(cli
, "L2TPNS %s", VERSION
);
799 char const *p
= strchr(cvs_name
, ':');
811 e
= strpbrk(p
, " \t$");
812 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
817 extern linked_list
*loaded_plugins
;
820 cli_print(cli
, "Files:");
821 cli_print(cli
, " %s", cvs_id_arp
);
823 cli_print(cli
, " %s", cvs_id_bgp
);
825 cli_print(cli
, " %s", cvs_id_cli
);
826 cli_print(cli
, " %s", cvs_id_cluster
);
827 cli_print(cli
, " %s", cvs_id_constants
);
828 cli_print(cli
, " %s", cvs_id_control
);
829 cli_print(cli
, " %s", cvs_id_icmp
);
830 cli_print(cli
, " %s", cvs_id_l2tpns
);
831 cli_print(cli
, " %s", cvs_id_ll
);
832 cli_print(cli
, " %s", cvs_id_ppp
);
833 cli_print(cli
, " %s", cvs_id_radius
);
834 cli_print(cli
, " %s", cvs_id_tbf
);
835 cli_print(cli
, " %s", cvs_id_util
);
837 ll_reset(loaded_plugins
);
838 while ((p
= ll_next(loaded_plugins
)))
840 char const **id
= dlsym(p
, "cvs_id");
842 cli_print(cli
, " %s", *id
);
849 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
852 int used
= 0, free
= 0, show_all
= 0;
854 if (!config
->cluster_iam_master
)
856 cli_print(cli
, "Can't do this on a slave. Do it on %s",
857 fmtaddr(config
->cluster_master_address
, 0));
862 if (CLI_HELP_REQUESTED
)
865 return cli_arg_help(cli
, 1, NULL
);
867 return cli_arg_help(cli
, 1,
868 "all", "Show all pool addresses, including unused",
872 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
876 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
877 for (i
= 0; i
< MAXIPPOOL
; i
++)
879 if (!ip_address_pool
[i
].address
) continue;
880 if (ip_address_pool
[i
].assigned
)
882 cli_print(cli
, "%-15s\tY %8d %s",
883 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
884 ip_address_pool
[i
].session
,
885 session
[ip_address_pool
[i
].session
].user
);
891 if (ip_address_pool
[i
].last
)
892 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
893 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
894 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
897 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
904 cli_print(cli
, "(Not displaying unused addresses)");
906 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
910 static FILE *save_config_fh
= 0;
911 static void print_save_config(struct cli_def
*cli
, char *string
)
914 fprintf(save_config_fh
, "%s\n", string
);
917 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
919 if (CLI_HELP_REQUESTED
)
920 return CLI_HELP_NO_ARGS
;
922 if ((save_config_fh
= fopen(config
->config_file
, "w")))
924 cli_print(cli
, "Writing configuration");
925 cli_print_callback(cli
, print_save_config
);
926 cmd_show_run(cli
, command
, argv
, argc
);
927 cli_print_callback(cli
, NULL
);
928 fclose(save_config_fh
);
933 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
938 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
940 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
943 char ipv6addr
[INET6_ADDRSTRLEN
];
945 if (CLI_HELP_REQUESTED
)
946 return CLI_HELP_NO_ARGS
;
948 cli_print(cli
, "# Current configuration:");
950 for (i
= 0; config_values
[i
].key
; i
++)
952 void *value
= ((void *)config
) + config_values
[i
].offset
;
953 if (config_values
[i
].type
== STRING
)
954 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
955 else if (config_values
[i
].type
== IPv4
)
956 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
957 else if (config_values
[i
].type
== IPv6
)
958 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
959 else if (config_values
[i
].type
== SHORT
)
960 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
961 else if (config_values
[i
].type
== BOOL
)
962 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
963 else if (config_values
[i
].type
== INT
)
964 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
965 else if (config_values
[i
].type
== UNSIGNED_LONG
)
966 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
969 cli_print(cli
, "# Plugins");
970 for (i
= 0; i
< MAXPLUGINS
; i
++)
972 if (*config
->plugins
[i
])
974 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
979 if (config
->as_number
)
984 cli_print(cli
, "# BGP");
985 cli_print(cli
, "router bgp %u", config
->as_number
);
986 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
988 if (!config
->neighbour
[i
].name
[0])
991 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
993 k
= config
->neighbour
[i
].keepalive
;
994 h
= config
->neighbour
[i
].hold
;
1001 k
= BGP_KEEPALIVE_TIME
;
1007 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1012 cli_print(cli
, "# Filters");
1013 for (i
= 0; i
< MAXFILTER
; i
++)
1015 ip_filter_rulet
*rules
;
1016 if (!*ip_filters
[i
].name
)
1019 cli_print(cli
, "ip access-list %s %s",
1020 ip_filters
[i
].extended
? "extended" : "standard",
1021 ip_filters
[i
].name
);
1023 rules
= ip_filters
[i
].rules
;
1024 while (rules
->action
)
1025 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1028 cli_print(cli
, "# end");
1032 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1034 int i
, free
= 0, used
= 0, show_all
= 0;
1046 if (CLI_HELP_REQUESTED
)
1049 return cli_arg_help(cli
, 1, NULL
);
1051 return cli_arg_help(cli
, 1,
1052 "all", "Show all RADIUS sessions, including unused",
1056 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1060 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1063 for (i
= 1; i
< MAXRADIUS
; i
++)
1065 if (radius
[i
].state
== RADIUSNULL
)
1070 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1072 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1076 states
[radius
[i
].state
],
1082 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1087 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1091 if (CLI_HELP_REQUESTED
)
1092 return CLI_HELP_NO_ARGS
;
1094 cli_print(cli
, "Plugins currently loaded:");
1095 for (i
= 0; i
< MAXPLUGINS
; i
++)
1096 if (*config
->plugins
[i
])
1097 cli_print(cli
, " %s", config
->plugins
[i
]);
1102 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1106 if (CLI_HELP_REQUESTED
)
1107 return CLI_HELP_NO_ARGS
;
1109 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1118 for (i
= 0; i
< MAXSESSION
; i
++)
1120 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1121 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1125 session
[i
].throttle_in
,
1126 session
[i
].throttle_out
,
1128 session
[i
].tbf_out
);
1134 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1136 if (CLI_HELP_REQUESTED
)
1137 return CLI_HELP_NO_ARGS
;
1139 cli_print(cli
, " _\n"
1145 " ( \\ `. `-. _,.-:\\\n"
1146 " \\ \\ `. `-._ __..--' ,-';/\n"
1147 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1148 " `. `. `-._ __..--' ,' /\n"
1149 " `. `-_ ``--..'' _.-' ,'\n"
1150 " `-_ `-.___ __,--' ,'\n"
1151 " `-.__ `----\"\"\" __.-'\n"
1152 "hh `--..____..--'");
1157 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1162 if (CLI_HELP_REQUESTED
)
1163 return cli_arg_help(cli
, argc
> 1,
1164 "USER", "Username of session to drop", NULL
);
1166 if (!config
->cluster_iam_master
)
1168 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1169 fmtaddr(config
->cluster_master_address
, 0));
1176 cli_error(cli
, "Specify a user to drop");
1180 for (i
= 0; i
< argc
; i
++)
1182 if (!(s
= sessionbyuser(argv
[i
])))
1184 cli_error(cli
, "User %s is not connected", argv
[i
]);
1188 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1190 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1191 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1198 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1203 if (CLI_HELP_REQUESTED
)
1204 return cli_arg_help(cli
, argc
> 1,
1205 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1207 if (!config
->cluster_iam_master
)
1209 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1210 fmtaddr(config
->cluster_master_address
, 0));
1217 cli_error(cli
, "Specify a tunnel to drop");
1221 for (i
= 0; i
< argc
; i
++)
1223 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1225 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1231 cli_error(cli
, "Tunnel %d is not connected", t
);
1237 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1241 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1242 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1248 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1253 if (CLI_HELP_REQUESTED
)
1254 return cli_arg_help(cli
, argc
> 1,
1255 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1257 if (!config
->cluster_iam_master
)
1259 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1260 fmtaddr(config
->cluster_master_address
, 0));
1267 cli_error(cli
, "Specify a session id to drop");
1271 for (i
= 0; i
< argc
; i
++)
1273 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1275 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1279 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1281 cli_print(cli
, "Dropping session %d", s
);
1282 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1286 cli_error(cli
, "Session %d is not active.", s
);
1293 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1299 if (CLI_HELP_REQUESTED
)
1304 return cli_arg_help(cli
, 0,
1305 "USER", "Username of session to snoop", NULL
);
1308 return cli_arg_help(cli
, 0,
1309 "A.B.C.D", "IP address of snoop destination", NULL
);
1312 return cli_arg_help(cli
, 0,
1313 "N", "Port of snoop destination", NULL
);
1317 return cli_arg_help(cli
, 1, NULL
);
1324 if (!config
->cluster_iam_master
)
1326 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1327 fmtaddr(config
->cluster_master_address
, 0));
1334 cli_error(cli
, "Specify username, ip and port");
1338 if (!(s
= sessionbyuser(argv
[0])))
1340 cli_error(cli
, "User %s is not connected", argv
[0]);
1344 ip
= inet_addr(argv
[1]);
1345 if (!ip
|| ip
== INADDR_NONE
)
1347 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1351 port
= atoi(argv
[2]);
1354 cli_error(cli
, "Invalid port %s", argv
[2]);
1358 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1359 cli_session_actions
[s
].snoop_ip
= ip
;
1360 cli_session_actions
[s
].snoop_port
= port
;
1361 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1366 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1371 if (CLI_HELP_REQUESTED
)
1372 return cli_arg_help(cli
, argc
> 1,
1373 "USER", "Username of session to unsnoop", NULL
);
1375 if (!config
->cluster_iam_master
)
1377 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1378 fmtaddr(config
->cluster_master_address
, 0));
1385 cli_error(cli
, "Specify a user to unsnoop");
1389 for (i
= 0; i
< argc
; i
++)
1391 if (!(s
= sessionbyuser(argv
[i
])))
1393 cli_error(cli
, "User %s is not connected", argv
[i
]);
1397 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1398 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1404 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1411 throttle USER - throttle in/out to default rate
1412 throttle USER RATE - throttle in/out to default rate
1413 throttle USER in RATE - throttle input only
1414 throttle USER out RATE - throttle output only
1415 throttle USER in RATE out RATE - throttle both
1418 if (CLI_HELP_REQUESTED
)
1423 return cli_arg_help(cli
, 0,
1424 "USER", "Username of session to throttle", NULL
);
1427 return cli_arg_help(cli
, 1,
1428 "RATE", "Rate in kbps (in and out)",
1429 "in", "Select incoming rate",
1430 "out", "Select outgoing rate", NULL
);
1433 return cli_arg_help(cli
, 1,
1434 "in", "Select incoming rate",
1435 "out", "Select outgoing rate", NULL
);
1438 if (isdigit(argv
[1][0]))
1439 return cli_arg_help(cli
, 1, NULL
);
1442 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1445 return cli_arg_help(cli
, argc
> 1, NULL
);
1449 if (!config
->cluster_iam_master
)
1451 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1452 fmtaddr(config
->cluster_master_address
, 0));
1459 cli_error(cli
, "Specify a user to throttle");
1463 if (!(s
= sessionbyuser(argv
[0])))
1465 cli_error(cli
, "User %s is not connected", argv
[0]);
1471 rate_in
= rate_out
= config
->rl_rate
;
1475 rate_in
= rate_out
= atoi(argv
[1]);
1478 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1482 else if (argc
== 3 || argc
== 5)
1485 for (i
= 1; i
< argc
- 1; i
+= 2)
1488 if (MATCH("in", argv
[i
]))
1489 r
= rate_in
= atoi(argv
[i
+1]);
1490 else if (MATCH("out", argv
[i
]))
1491 r
= rate_out
= atoi(argv
[i
+1]);
1495 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1502 cli_error(cli
, "Invalid arguments");
1506 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1508 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1512 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1513 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1514 cli_session_actions
[s
].throttle_in
= rate_in
;
1516 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1517 cli_session_actions
[s
].throttle_out
= rate_out
;
1519 if (cli_session_actions
[s
].throttle_in
== -1 &&
1520 cli_session_actions
[s
].throttle_out
== -1)
1522 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1526 cli_print(cli
, "Throttling user %s", argv
[0]);
1527 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1532 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1537 if (CLI_HELP_REQUESTED
)
1538 return cli_arg_help(cli
, argc
> 1,
1539 "USER", "Username of session to unthrottle", NULL
);
1541 if (!config
->cluster_iam_master
)
1543 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1544 fmtaddr(config
->cluster_master_address
, 0));
1551 cli_error(cli
, "Specify a user to unthrottle");
1555 for (i
= 0; i
< argc
; i
++)
1557 if (!(s
= sessionbyuser(argv
[i
])))
1559 cli_error(cli
, "User %s is not connected", argv
[i
]);
1563 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1565 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1566 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1570 cli_error(cli
, "User %s not throttled", argv
[i
]);
1577 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1581 if (CLI_HELP_REQUESTED
)
1582 return cli_arg_help(cli
, 1,
1583 "all", "Enable debugging for all except \"data\"",
1584 "critical", "", // FIXME: add descriptions
1594 char *p
= (char *) &debug_flags
;
1595 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1599 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1600 (debug_flags
.critical
) ? " critical" : "",
1601 (debug_flags
.error
) ? " error" : "",
1602 (debug_flags
.warning
) ? " warning" : "",
1603 (debug_flags
.info
) ? " info" : "",
1604 (debug_flags
.calls
) ? " calls" : "",
1605 (debug_flags
.data
) ? " data" : "");
1611 cli_print(cli
, "Debugging off");
1615 for (i
= 0; i
< argc
; i
++)
1617 int len
= strlen(argv
[i
]);
1619 if (argv
[i
][0] == 'c' && len
< 2)
1620 len
= 2; /* distinguish [cr]itical from [ca]lls */
1622 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1623 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1624 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1625 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1626 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1627 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1628 if (!strncmp("all", argv
[i
], len
))
1630 memset(&debug_flags
, 1, sizeof(debug_flags
));
1631 debug_flags
.data
= 0;
1635 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1641 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1645 if (CLI_HELP_REQUESTED
)
1646 return cli_arg_help(cli
, 1,
1647 "all", "Disable all debugging",
1648 "critical", "", // FIXME: add descriptions
1658 memset(&debug_flags
, 0, sizeof(debug_flags
));
1662 for (i
= 0; i
< argc
; i
++)
1664 int len
= strlen(argv
[i
]);
1666 if (argv
[i
][0] == 'c' && len
< 2)
1667 len
= 2; /* distinguish [cr]itical from [ca]lls */
1669 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1670 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1671 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1672 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1673 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1674 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1675 if (!strncmp("all", argv
[i
], len
))
1677 memset(&debug_flags
, 0, sizeof(debug_flags
));
1681 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1687 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1689 int i
, firstfree
= 0;
1691 if (CLI_HELP_REQUESTED
)
1692 return cli_arg_help(cli
, argc
> 1,
1693 "PLUGIN", "Name of plugin to load", NULL
);
1697 cli_error(cli
, "Specify a plugin to load");
1701 for (i
= 0; i
< MAXPLUGINS
; i
++)
1703 if (!*config
->plugins
[i
] && !firstfree
)
1705 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1707 cli_error(cli
, "Plugin is already loaded");
1714 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1715 config
->reload_config
= 1;
1716 cli_print(cli
, "Loading plugin %s", argv
[0]);
1722 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1726 if (CLI_HELP_REQUESTED
)
1727 return cli_arg_help(cli
, argc
> 1,
1728 "PLUGIN", "Name of plugin to unload", NULL
);
1732 cli_error(cli
, "Specify a plugin to remove");
1736 for (i
= 0; i
< MAXPLUGINS
; i
++)
1738 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1740 config
->reload_config
= 1;
1741 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1746 cli_error(cli
, "Plugin is not loaded");
1750 static char *duration(time_t secs
)
1752 static char *buf
= NULL
;
1755 if (!buf
) buf
= calloc(64, 1);
1759 int days
= secs
/ 86400;
1760 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1766 int mins
= secs
/ 60;
1767 int hrs
= mins
/ 60;
1770 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1772 else if (secs
>= 60)
1774 int mins
= secs
/ 60;
1775 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1778 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1783 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1786 char buf
[100], *p
= buf
, *loads
[3];
1787 int i
, num_sessions
= 0;
1789 if (CLI_HELP_REQUESTED
)
1790 return CLI_HELP_NO_ARGS
;
1792 fh
= fopen("/proc/loadavg", "r");
1793 fgets(buf
, 100, fh
);
1796 for (i
= 0; i
< 3; i
++)
1797 loads
[i
] = strdup(strsep(&p
, " "));
1800 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1802 for (i
= 1; i
< MAXSESSION
; i
++)
1803 if (session
[i
].opened
) num_sessions
++;
1805 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1807 duration(time_now
- config
->start_time
),
1809 loads
[0], loads
[1], loads
[2]
1811 for (i
= 0; i
< 3; i
++)
1812 if (loads
[i
]) free(loads
[i
]);
1814 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1819 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1823 if (CLI_HELP_REQUESTED
)
1829 int len
= strlen(argv
[0])-1;
1830 for (i
= 0; config_values
[i
].key
; i
++)
1831 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1832 cli_error(cli
, " %s", config_values
[i
].key
);
1838 return cli_arg_help(cli
, 0,
1839 "VALUE", "Value for variable", NULL
);
1843 return cli_arg_help(cli
, 1, NULL
);
1852 cli_error(cli
, "Specify variable and value");
1856 for (i
= 0; config_values
[i
].key
; i
++)
1858 void *value
= ((void *) config
) + config_values
[i
].offset
;
1859 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1861 // Found a value to set
1862 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1863 switch (config_values
[i
].type
)
1866 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1869 *(int *) value
= atoi(argv
[1]);
1872 *(unsigned long *) value
= atol(argv
[1]);
1875 *(short *) value
= atoi(argv
[1]);
1878 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1881 inet_pton(AF_INET6
, argv
[1], value
);
1884 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1890 cli_error(cli
, "Unknown variable type");
1893 config
->reload_config
= 1;
1898 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1902 int regular_stuff(struct cli_def
*cli
)
1908 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1910 char *m
= ringbuffer
->buffer
[i
].message
;
1916 switch (ringbuffer
->buffer
[i
].level
)
1918 case 0: show
= debug_flags
.critical
; break;
1919 case 1: show
= debug_flags
.error
; break;
1920 case 2: show
= debug_flags
.warning
; break;
1921 case 3: show
= debug_flags
.info
; break;
1922 case 4: show
= debug_flags
.calls
; break;
1923 case 5: show
= debug_flags
.data
; break;
1926 if (!show
) continue;
1928 if (!(p
= strchr(m
, '\n')))
1931 cli_error(cli
, "\r%s-%u-%u %.*s",
1932 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1933 ringbuffer
->buffer
[i
].tunnel
,
1934 ringbuffer
->buffer
[i
].session
,
1940 debug_rb_tail
= ringbuffer
->tail
;
1948 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1952 if (CLI_HELP_REQUESTED
)
1953 return cli_arg_help(cli
, argc
> 1,
1954 "<1-65535>", "Autonomous system number", NULL
);
1956 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1958 cli_error(cli
, "Invalid autonomous system number");
1962 if (bgp_configured
&& as
!= config
->as_number
)
1964 cli_error(cli
, "Can't change local AS on a running system");
1968 config
->as_number
= as
;
1969 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1974 static int find_bgp_neighbour(char const *name
)
1979 in_addr_t addrs
[4] = { 0 };
1982 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1985 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1986 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1988 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1990 if (!config
->neighbour
[i
].name
[0])
1992 if (new == -1) new = i
;
1996 if (!strcmp(name
, config
->neighbour
[i
].name
))
1999 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
2002 for (a
= h
->h_addr_list
; *a
; a
++)
2005 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
2006 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2014 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2020 if (CLI_HELP_REQUESTED
)
2025 return cli_arg_help(cli
, 0,
2026 "A.B.C.D", "BGP neighbour address",
2027 "NAME", "BGP neighbour name",
2031 return cli_arg_help(cli
, 0,
2032 "remote-as", "Set remote autonomous system number",
2033 "timers", "Set timers",
2037 if (MATCH("remote-as", argv
[1]))
2038 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2040 if (MATCH("timers", argv
[1]))
2043 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2046 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2048 if (argc
== 5 && !argv
[4][1])
2049 return cli_arg_help(cli
, 1, NULL
);
2058 cli_error(cli
, "Invalid arguments");
2062 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2064 cli_error(cli
, "Invalid neighbour");
2070 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2074 if (MATCH("remote-as", argv
[1]))
2076 int as
= atoi(argv
[2]);
2077 if (as
< 0 || as
> 65535)
2079 cli_error(cli
, "Invalid autonomous system number");
2083 if (!config
->neighbour
[i
].name
[0])
2085 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2086 config
->neighbour
[i
].keepalive
= -1;
2087 config
->neighbour
[i
].hold
= -1;
2090 config
->neighbour
[i
].as
= as
;
2094 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2096 cli_error(cli
, "Invalid arguments");
2100 if (!config
->neighbour
[i
].name
[0])
2102 cli_error(cli
, "Specify remote-as first");
2106 keepalive
= atoi(argv
[2]);
2107 hold
= atoi(argv
[3]);
2109 if (keepalive
< 1 || keepalive
> 65535)
2111 cli_error(cli
, "Invalid keepalive time");
2115 if (hold
< 3 || hold
> 65535)
2117 cli_error(cli
, "Invalid hold time");
2121 if (keepalive
== BGP_KEEPALIVE_TIME
)
2122 keepalive
= -1; // using default value
2124 if (hold
== BGP_HOLD_TIME
)
2127 config
->neighbour
[i
].keepalive
= keepalive
;
2128 config
->neighbour
[i
].hold
= hold
;
2133 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2137 if (CLI_HELP_REQUESTED
)
2138 return cli_arg_help(cli
, argc
> 0,
2139 "A.B.C.D", "BGP neighbour address",
2140 "NAME", "BGP neighbour name",
2145 cli_error(cli
, "Specify a BGP neighbour");
2149 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2151 cli_error(cli
, "Invalid neighbour");
2155 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2157 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2161 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2165 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2171 if (!bgp_configured
)
2174 if (CLI_HELP_REQUESTED
)
2175 return cli_arg_help(cli
, 1,
2176 "A.B.C.D", "BGP neighbour address",
2177 "NAME", "BGP neighbour name",
2180 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2181 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2185 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2187 if (!*bgp_peers
[i
].name
)
2190 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2191 if (argc
&& strcmp(addr
, argv
[0]) &&
2192 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2198 cli_print(cli
, "Peer AS Address "
2199 "State Retries Retry in Route Pend Timers");
2200 cli_print(cli
, "------------------ ----- --------------- "
2201 "----------- ------- -------- ----- ---- ---------");
2204 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2208 bgp_state_str(bgp_peers
[i
].state
),
2209 bgp_peers
[i
].retry_count
,
2210 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2211 bgp_peers
[i
].routing
? "yes" : "no",
2212 bgp_peers
[i
].update_routes
? "yes" : "no",
2213 bgp_peers
[i
].keepalive
,
2220 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2225 if (!bgp_configured
)
2228 if (CLI_HELP_REQUESTED
)
2229 return cli_arg_help(cli
, 1,
2230 "A.B.C.D", "BGP neighbour address",
2231 "NAME", "BGP neighbour name",
2234 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2236 if (bgp_peers
[i
].state
!= Established
)
2239 if (!bgp_peers
[i
].routing
)
2242 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2243 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2246 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2247 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2253 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2258 if (!bgp_configured
)
2261 if (CLI_HELP_REQUESTED
)
2262 return cli_arg_help(cli
, 1,
2263 "A.B.C.D", "BGP neighbour address",
2264 "NAME", "BGP neighbour name",
2267 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2269 if (bgp_peers
[i
].state
!= Established
)
2272 if (bgp_peers
[i
].routing
)
2275 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2276 if (argc
&& strcmp(addr
, argv
[0]) &&
2277 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2280 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2281 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2287 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2292 if (!bgp_configured
)
2295 if (CLI_HELP_REQUESTED
)
2296 return cli_arg_help(cli
, 1,
2297 "A.B.C.D", "BGP neighbour address",
2298 "NAME", "BGP neighbour name",
2301 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2303 if (!*bgp_peers
[i
].name
)
2306 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2307 if (argc
&& strcmp(addr
, argv
[0]) &&
2308 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2311 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2312 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2320 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2324 if (CLI_HELP_REQUESTED
)
2329 return cli_arg_help(cli
, 0,
2330 "standard", "Standard syntax",
2331 "extended", "Extended syntax",
2335 return cli_arg_help(cli
, argv
[1][1],
2336 "NAME", "Access-list name",
2340 if (argc
== 3 && !argv
[2][1])
2341 return cli_arg_help(cli
, 1, NULL
);
2349 cli_error(cli
, "Specify access-list type and name");
2353 if (MATCH("standard", argv
[0]))
2355 else if (MATCH("extended", argv
[0]))
2359 cli_error(cli
, "Invalid access-list type");
2363 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2364 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2366 cli_error(cli
, "Invalid access-list name");
2370 filt
= find_filter(argv
[1], strlen(argv
[1]));
2375 cli_error(cli
, "Too many access-lists");
2380 if (!*ip_filters
[filt
].name
)
2382 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2383 strcpy(ip_filters
[filt
].name
, argv
[1]);
2384 ip_filters
[filt
].extended
= extended
;
2386 else if (ip_filters
[filt
].extended
!= extended
)
2388 cli_error(cli
, "Access-list is %s",
2389 ip_filters
[filt
].extended
? "extended" : "standard");
2394 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2398 if (filt
< 0 || !*ip_filters
[filt
].name
)
2400 cli_error(cli
, "Access-list not defined");
2405 if (ip_filters
[filt
].used
)
2407 cli_error(cli
, "Access-list in use");
2411 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2415 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2417 return access_list(cli
, argv
, argc
, 1);
2420 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2422 return access_list(cli
, argv
, argc
, 0);
2425 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2427 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2428 return sprintf(buf
, " any");
2430 if (wild
== INADDR_ANY
)
2431 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2433 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2436 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2440 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2441 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2442 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2443 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2444 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2450 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2452 static char buf
[256];
2455 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2458 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2459 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2462 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2466 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2467 p
+= show_ports(p
, &rule
->src_ports
);
2469 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2470 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2471 p
+= show_ports(p
, &rule
->dst_ports
);
2473 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2475 switch (rule
->tcp_flag_op
)
2477 case FILTER_FLAG_OP_EST
:
2478 p
+= sprintf(p
, " established");
2481 case FILTER_FLAG_OP_ANY
:
2482 case FILTER_FLAG_OP_ALL
:
2483 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2484 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2485 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2486 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2487 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2488 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2489 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2490 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2491 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2492 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2493 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2494 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2495 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2501 p
+= sprintf(p
, " fragments");
2506 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2508 static ip_filter_rulet rule
;
2509 struct in_addr addr
;
2513 if (CLI_HELP_REQUESTED
)
2517 cli_arg_help(cli
, 0,
2518 "ip", "Match IP packets",
2519 "tcp", "Match TCP packets",
2520 "udp", "Match UDP packets",
2526 // *sigh*, too darned complex
2527 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2533 cli_error(cli
, "Specify rule details");
2537 memset(&rule
, 0, sizeof(rule
));
2538 rule
.action
= (command
[0] == 'p')
2539 ? FILTER_ACTION_PERMIT
2540 : FILTER_ACTION_DENY
;
2542 if (MATCH("ip", argv
[0]))
2543 rule
.proto
= IPPROTO_IP
;
2544 else if (MATCH("udp", argv
[0]))
2545 rule
.proto
= IPPROTO_UDP
;
2546 else if (MATCH("tcp", argv
[0]))
2547 rule
.proto
= IPPROTO_TCP
;
2550 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2554 for (a
= 1, i
= 0; i
< 2; i
++)
2558 ip_filter_portt
*port
;
2563 wild
= &rule
.src_wild
;
2564 port
= &rule
.src_ports
;
2569 wild
= &rule
.dst_wild
;
2570 port
= &rule
.dst_ports
;
2573 cli_error(cli
, "Specify destination");
2578 if (MATCH("any", argv
[a
]))
2581 *wild
= INADDR_BROADCAST
;
2584 else if (MATCH("host", argv
[a
]))
2588 cli_error(cli
, "Specify host ip address");
2592 if (!inet_aton(argv
[a
], &addr
))
2594 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2606 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2610 if (!inet_aton(argv
[a
], &addr
))
2612 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2618 if (!inet_aton(argv
[++a
], &addr
))
2620 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2624 *wild
= addr
.s_addr
;
2628 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2632 if (MATCH("eq", argv
[a
]))
2633 port
->op
= FILTER_PORT_OP_EQ
;
2634 else if (MATCH("neq", argv
[a
]))
2635 port
->op
= FILTER_PORT_OP_NEQ
;
2636 else if (MATCH("gt", argv
[a
]))
2637 port
->op
= FILTER_PORT_OP_GT
;
2638 else if (MATCH("lt", argv
[a
]))
2639 port
->op
= FILTER_PORT_OP_LT
;
2640 else if (MATCH("range", argv
[a
]))
2641 port
->op
= FILTER_PORT_OP_RANGE
;
2648 cli_error(cli
, "Specify port");
2652 if (!(port
->port
= atoi(argv
[a
])))
2654 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2659 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2664 cli_error(cli
, "Specify port");
2668 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2670 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2677 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2679 if (MATCH("established", argv
[a
]))
2681 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2684 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2685 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2687 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2688 ? FILTER_FLAG_OP_ANY
2689 : FILTER_FLAG_OP_ALL
;
2693 cli_error(cli
, "Specify tcp flags");
2697 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2701 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2703 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2704 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2705 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2706 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2707 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2708 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2711 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2720 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2722 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2724 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2734 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2741 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2743 static ip_filter_rulet rule
;
2744 struct in_addr addr
;
2746 if (CLI_HELP_REQUESTED
)
2750 cli_arg_help(cli
, argv
[0][1],
2751 "A.B.C.D", "Source address",
2752 "any", "Any source address",
2753 "host", "Source host",
2759 if (MATCH("any", argv
[0]))
2761 if (argc
== 2 && !argv
[1][1])
2762 cli_arg_help(cli
, 1, NULL
);
2764 else if (MATCH("host", argv
[0]))
2768 cli_arg_help(cli
, argv
[1][1],
2769 "A.B.C.D", "Host address",
2772 else if (argc
== 3 && !argv
[2][1])
2773 cli_arg_help(cli
, 1, NULL
);
2779 cli_arg_help(cli
, 1,
2780 "A.B.C.D", "Wildcard bits",
2783 else if (argc
== 3 && !argv
[2][1])
2784 cli_arg_help(cli
, 1, NULL
);
2792 cli_error(cli
, "Specify rule details");
2796 memset(&rule
, 0, sizeof(rule
));
2797 rule
.action
= (command
[0] == 'p')
2798 ? FILTER_ACTION_PERMIT
2799 : FILTER_ACTION_DENY
;
2801 rule
.proto
= IPPROTO_IP
;
2802 if (MATCH("any", argv
[0]))
2804 rule
.src_ip
= INADDR_ANY
;
2805 rule
.src_wild
= INADDR_BROADCAST
;
2807 else if (MATCH("host", argv
[0]))
2811 cli_error(cli
, "Specify host ip address");
2815 if (!inet_aton(argv
[1], &addr
))
2817 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2821 rule
.src_ip
= addr
.s_addr
;
2822 rule
.src_wild
= INADDR_ANY
;
2828 cli_error(cli
, "Specify source ip address and wildcard");
2832 if (!inet_aton(argv
[0], &addr
))
2834 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2838 rule
.src_ip
= addr
.s_addr
;
2842 if (!inet_aton(argv
[1], &addr
))
2844 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2848 rule
.src_wild
= addr
.s_addr
;
2851 rule
.src_wild
= INADDR_ANY
;
2857 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2860 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2861 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2862 : access_list_rule_std(cli
, command
, argv
, argc
);
2867 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2869 if (!ip_filters
[filt
].rules
[i
].action
)
2871 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2875 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2879 cli_error(cli
, "Too many rules");
2883 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2888 /* filter USER {in|out} FILTER ... */
2889 if (CLI_HELP_REQUESTED
)
2894 return cli_arg_help(cli
, 0,
2895 "USER", "Username of session to filter", NULL
);
2899 return cli_arg_help(cli
, 0,
2900 "in", "Set incoming filter",
2901 "out", "Set outgoing filter", NULL
);
2905 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2906 "NAME", "Filter name", NULL
);
2909 return cli_arg_help(cli
, argc
> 1, NULL
);
2913 if (!config
->cluster_iam_master
)
2915 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2916 fmtaddr(config
->cluster_master_address
, 0));
2921 if (argc
!= 3 && argc
!= 5)
2923 cli_error(cli
, "Specify a user and filters");
2927 if (!(s
= sessionbyuser(argv
[0])))
2929 cli_error(cli
, "User %s is not connected", argv
[0]);
2933 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2934 for (i
= 1; i
< argc
; i
+= 2)
2939 if (MATCH("in", argv
[i
]))
2941 if (session
[s
].filter_in
)
2943 cli_error(cli
, "Input already filtered");
2946 f
= &cli_session_actions
[s
].filter_in
;
2948 else if (MATCH("out", argv
[i
]))
2950 if (session
[s
].filter_out
)
2952 cli_error(cli
, "Output already filtered");
2955 f
= &cli_session_actions
[s
].filter_out
;
2959 cli_error(cli
, "Invalid filter specification");
2963 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
2964 if (v
< 0 || !*ip_filters
[v
].name
)
2966 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
2973 cli_print(cli
, "Filtering user %s", argv
[0]);
2974 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2979 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2984 if (CLI_HELP_REQUESTED
)
2985 return cli_arg_help(cli
, argc
> 1,
2986 "USER", "Username of session to remove filters from", NULL
);
2988 if (!config
->cluster_iam_master
)
2990 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2991 fmtaddr(config
->cluster_master_address
, 0));
2998 cli_error(cli
, "Specify a user to remove filters from");
3002 for (i
= 0; i
< argc
; i
++)
3004 if (!(s
= sessionbyuser(argv
[i
])))
3006 cli_error(cli
, "User %s is not connected", argv
[i
]);
3010 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3012 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3013 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3017 cli_error(cli
, "User %s not filtered", argv
[i
]);
3024 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3028 if (CLI_HELP_REQUESTED
)
3029 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3033 cli_error(cli
, "Specify a filter name");
3037 for (i
= 0; i
< argc
; i
++)
3039 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
3040 ip_filter_rulet
*rules
;
3042 if (f
< 0 || !*ip_filters
[f
].name
)
3044 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3051 cli_print(cli
, "%s IP access list %s",
3052 ip_filters
[f
].extended
? "Extended" : "Standard",
3053 ip_filters
[f
].name
);
3055 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3057 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3059 cli_print(cli
, "%s (%u match%s)", r
,
3060 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3062 cli_print(cli
, "%s", r
);