1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.71 2005/12/06 09:43:42 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
);
106 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
107 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 #endif /* STATISTICS */
111 #define MODE_CONFIG_BGP 8
112 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
113 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
114 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
115 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
116 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
121 #define MODE_CONFIG_NACL 9
122 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
123 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
124 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
125 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
126 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
127 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
129 /* match if b is a substr of a */
130 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
132 void init_cli(char *hostname
)
136 struct cli_command
*c
;
137 struct cli_command
*c2
;
139 struct sockaddr_in addr
;
142 if (hostname
&& *hostname
)
143 cli_set_hostname(cli
, hostname
);
145 cli_set_hostname(cli
, "l2tpns");
147 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
148 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
150 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
152 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
153 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
154 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
155 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
156 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
157 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
158 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
159 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
160 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
161 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
162 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");
163 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
164 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
166 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
167 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
168 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
171 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
173 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
174 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
177 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
179 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
180 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
181 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
183 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
184 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
185 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
186 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
189 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
190 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
193 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
194 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
195 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
196 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
197 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
200 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
201 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
203 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
204 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
206 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
207 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
209 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
211 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
212 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
215 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
216 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
217 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
218 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
220 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
221 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
223 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
224 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
226 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
228 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
229 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
231 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
232 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
234 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
235 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
236 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
238 // Enable regular processing
239 cli_regular(cli
, regular_stuff
);
241 if (!(f
= fopen(CLIUSERS
, "r")))
243 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
247 while (fgets(buf
, 4096, f
))
250 if (*buf
== '#') continue;
251 if ((p
= strchr(buf
, '\r'))) *p
= 0;
252 if ((p
= strchr(buf
, '\n'))) *p
= 0;
254 if (!(p
= strchr((char *)buf
, ':'))) continue;
256 if (!strcmp(buf
, "enable"))
258 cli_allow_enable(cli
, p
);
259 LOG(3, 0, 0, "Setting enable password\n");
263 cli_allow_user(cli
, buf
, p
);
264 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
270 memset(&addr
, 0, sizeof(addr
));
271 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
272 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
275 // Set cli fd as non-blocking
276 flags
= fcntl(clifd
, F_GETFL
, 0);
277 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
279 addr
.sin_family
= AF_INET
;
280 addr
.sin_port
= htons(23);
281 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
283 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
289 void cli_do(int sockfd
)
291 int require_auth
= 1;
292 struct sockaddr_in addr
;
293 socklen_t l
= sizeof(addr
);
295 if (fork_and_close()) return;
296 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
298 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
299 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
300 fmtaddr(addr
.sin_addr
.s_addr
, 0));
303 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
307 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
308 if (!cli
->users
) /* paranoia */
310 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
316 /* no username/pass required */
321 debug_rb_tail
= ringbuffer
->tail
;
323 memset(&debug_flags
, 0, sizeof(debug_flags
));
324 debug_flags
.critical
= 1;
326 cli_loop(cli
, sockfd
);
329 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
330 fmtaddr(addr
.sin_addr
.s_addr
, 0));
335 static void cli_print_log(struct cli_def
*cli
, char *string
)
337 LOG(3, 0, 0, "%s\n", string
);
340 void cli_do_file(FILE *fh
)
342 LOG(3, 0, 0, "Reading configuration file\n");
343 cli_print_callback(cli
, cli_print_log
);
344 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
345 cli_print_callback(cli
, NULL
);
348 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
359 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
361 int v
= va_arg(ap
, int);
362 snprintf(buf
, sizeof(buf
), entry
, v
);
368 desc
= va_arg(ap
, char *);
370 cli_error(cli
, " %-20s %s", p
, desc
);
372 cli_error(cli
, " %s", p
);
374 entry
= desc
? va_arg(ap
, char *) : 0;
379 cli_error(cli
, " <cr>");
384 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
388 if (CLI_HELP_REQUESTED
)
389 return cli_arg_help(cli
, 1,
390 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
396 // Show individual session
397 for (i
= 0; i
< argc
; i
++)
399 unsigned int s
, b_in
, b_out
;
401 if (s
<= 0 || s
>= MAXSESSION
)
403 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
406 cli_print(cli
, "\r\nSession %d:", s
);
407 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
408 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
409 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
410 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
411 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
412 switch (session
[s
].ppp
.phase
)
415 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
420 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
421 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
422 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
424 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
425 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
426 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
427 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
428 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
429 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
430 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
431 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
432 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
433 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
434 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
435 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
436 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
437 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
438 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
440 cli_print(cli
, "\tIntercepted:\tno");
442 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
444 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
445 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
446 t
? "YES" : "no", t
? " (" : "",
447 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
449 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
453 b_in
= session
[s
].tbf_in
;
454 b_out
= session
[s
].tbf_out
;
456 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
457 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
458 "ByteSent", "PackSent", "PackDrop", "PackDelay");
461 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
463 (filter_list
[b_in
].next
? "*" : " "),
464 (b_in
< 100 ? "\t" : ""),
465 filter_list
[b_in
].rate
* 8,
466 filter_list
[b_in
].credit
,
467 filter_list
[b_in
].queued
,
468 filter_list
[b_in
].b_queued
,
469 filter_list
[b_in
].p_queued
,
470 filter_list
[b_in
].b_sent
,
471 filter_list
[b_in
].p_sent
,
472 filter_list
[b_in
].p_dropped
,
473 filter_list
[b_in
].p_delayed
);
476 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
478 (filter_list
[b_out
].next
? "*" : " "),
479 (b_out
< 100 ? "\t" : ""),
480 filter_list
[b_out
].rate
* 8,
481 filter_list
[b_out
].credit
,
482 filter_list
[b_out
].queued
,
483 filter_list
[b_out
].b_queued
,
484 filter_list
[b_out
].p_queued
,
485 filter_list
[b_out
].b_sent
,
486 filter_list
[b_out
].p_sent
,
487 filter_list
[b_out
].p_dropped
,
488 filter_list
[b_out
].p_delayed
);
495 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %-15s %s",
511 for (i
= 1; i
< MAXSESSION
; i
++)
513 if (!session
[i
].opened
) continue;
514 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %-15s %s",
517 session
[i
].user
[0] ? session
[i
].user
: "*",
518 fmtaddr(htonl(session
[i
].ip
), 0),
519 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
520 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
521 (session
[i
].walled_garden
) ? "Y" : "N",
522 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
523 abs(time_now
- (unsigned long)session
[i
].opened
),
524 (unsigned long)session
[i
].cout
,
525 (unsigned long)session
[i
].cin
,
526 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
527 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
528 session
[i
].calling
[0] ? session
[i
].calling
: "*");
533 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
535 int i
, x
, show_all
= 0;
543 if (CLI_HELP_REQUESTED
)
546 return cli_arg_help(cli
, 1,
547 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
550 return cli_arg_help(cli
, 1,
551 "all", "Show all tunnels, including unused",
552 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
559 if (strcmp(argv
[0], "all") == 0)
565 // Show individual tunnel
566 for (i
= 0; i
< argc
; i
++)
571 if (t
<= 0 || t
>= MAXTUNNEL
)
573 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
576 cli_print(cli
, "\r\nTunnel %d:", t
);
577 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
578 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
579 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
580 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
581 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
582 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
583 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
584 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
585 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
587 for (x
= 0; x
< MAXSESSION
; x
++)
588 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
589 sprintf(s
, "%s%u ", s
, x
);
591 cli_print(cli
, "\tSessions:\t%s", s
);
597 // Show tunnel summary
598 cli_print(cli
, "%4s %20s %20s %6s %s",
605 for (i
= 1; i
< MAXTUNNEL
; i
++)
608 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
610 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
611 cli_print(cli
, "%4d %20s %20s %6s %6d",
613 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
614 fmtaddr(htonl(tunnel
[i
].ip
), 0),
615 states
[tunnel
[i
].state
],
622 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
629 if (CLI_HELP_REQUESTED
)
630 return cli_arg_help(cli
, 1,
631 "USER", "Show details for specific username",
634 for (i
= 0; i
< MAXSESSION
; i
++)
636 if (!session
[i
].opened
) continue;
637 if (!session
[i
].user
[0]) continue;
641 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
643 if (strcmp(argv
[j
], session
[i
].user
) == 0)
645 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
646 sargv
[sargc
] = sid
[sargc
];
654 cli_print(cli
, "%s", session
[i
].user
);
658 return cmd_show_session(cli
, "users", sargv
, sargc
);
664 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
666 if (CLI_HELP_REQUESTED
)
667 return CLI_HELP_NO_ARGS
;
669 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
670 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
671 GET_STAT(tun_rx_bytes
),
672 GET_STAT(tun_rx_packets
),
673 GET_STAT(tun_rx_errors
),
674 GET_STAT(tun_rx_dropped
));
675 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
676 GET_STAT(tun_tx_bytes
),
677 GET_STAT(tun_tx_packets
),
678 GET_STAT(tun_tx_errors
));
681 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
682 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
683 GET_STAT(tunnel_rx_bytes
),
684 GET_STAT(tunnel_rx_packets
),
685 GET_STAT(tunnel_rx_errors
));
686 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
687 GET_STAT(tunnel_tx_bytes
),
688 GET_STAT(tunnel_tx_packets
),
689 GET_STAT(tunnel_tx_errors
),
690 GET_STAT(tunnel_retries
));
693 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
694 cli_print(cli
, "-----------------------------------------");
695 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
696 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
697 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
698 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
699 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
700 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
701 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
702 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
703 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
704 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
705 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
706 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
707 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
708 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
709 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
710 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
711 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
712 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
716 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
717 cli_print(cli
, "-----------------------------------------");
718 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
719 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
720 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
721 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
722 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
723 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
724 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
725 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
726 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
727 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
728 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
729 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
730 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
731 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
732 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
733 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
734 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
735 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
736 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
737 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
738 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
739 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
740 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
741 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
742 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
743 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
744 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
745 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
746 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
747 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
748 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
749 #endif /* STAT_CALLS */
752 time_t l
= GET_STAT(last_reset
);
754 char *p
= strchr(t
, '\n');
758 cli_print(cli
, "Last counter reset %s", t
);
764 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
766 if (CLI_HELP_REQUESTED
)
767 return CLI_HELP_NO_ARGS
;
769 memset(_statistics
, 0, sizeof(struct Tstats
));
770 SET_STAT(last_reset
, time(NULL
));
772 cli_print(cli
, "Counters cleared");
775 #endif /* STATISTICS */
777 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
783 if (CLI_HELP_REQUESTED
)
784 return cli_arg_help(cli
, 1,
785 "tag", "Include CVS release tag",
786 "file", "Include file versions",
789 for (i
= 0; i
< argc
; i
++)
790 if (!strcmp(argv
[i
], "tag"))
792 else if (!strcmp(argv
[i
], "file"))
795 cli_print(cli
, "L2TPNS %s", VERSION
);
798 char const *p
= strchr(cvs_name
, ':');
810 e
= strpbrk(p
, " \t$");
811 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
816 extern linked_list
*loaded_plugins
;
819 cli_print(cli
, "Files:");
820 cli_print(cli
, " %s", cvs_id_arp
);
822 cli_print(cli
, " %s", cvs_id_bgp
);
824 cli_print(cli
, " %s", cvs_id_cli
);
825 cli_print(cli
, " %s", cvs_id_cluster
);
826 cli_print(cli
, " %s", cvs_id_constants
);
827 cli_print(cli
, " %s", cvs_id_control
);
828 cli_print(cli
, " %s", cvs_id_icmp
);
829 cli_print(cli
, " %s", cvs_id_l2tpns
);
830 cli_print(cli
, " %s", cvs_id_ll
);
831 cli_print(cli
, " %s", cvs_id_ppp
);
832 cli_print(cli
, " %s", cvs_id_radius
);
833 cli_print(cli
, " %s", cvs_id_tbf
);
834 cli_print(cli
, " %s", cvs_id_util
);
836 ll_reset(loaded_plugins
);
837 while ((p
= ll_next(loaded_plugins
)))
839 char const **id
= dlsym(p
, "cvs_id");
841 cli_print(cli
, " %s", *id
);
848 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
851 int used
= 0, free
= 0, show_all
= 0;
853 if (!config
->cluster_iam_master
)
855 cli_print(cli
, "Can't do this on a slave. Do it on %s",
856 fmtaddr(config
->cluster_master_address
, 0));
861 if (CLI_HELP_REQUESTED
)
864 return cli_arg_help(cli
, 1, NULL
);
866 return cli_arg_help(cli
, 1,
867 "all", "Show all pool addresses, including unused",
871 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
875 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
876 for (i
= 0; i
< MAXIPPOOL
; i
++)
878 if (!ip_address_pool
[i
].address
) continue;
879 if (ip_address_pool
[i
].assigned
)
881 cli_print(cli
, "%-15s\tY %8d %s",
882 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
883 ip_address_pool
[i
].session
,
884 session
[ip_address_pool
[i
].session
].user
);
890 if (ip_address_pool
[i
].last
)
891 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
892 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
893 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
896 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
903 cli_print(cli
, "(Not displaying unused addresses)");
905 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
909 static FILE *save_config_fh
= 0;
910 static void print_save_config(struct cli_def
*cli
, char *string
)
913 fprintf(save_config_fh
, "%s\n", string
);
916 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
918 if (CLI_HELP_REQUESTED
)
919 return CLI_HELP_NO_ARGS
;
921 if ((save_config_fh
= fopen(config
->config_file
, "w")))
923 cli_print(cli
, "Writing configuration");
924 cli_print_callback(cli
, print_save_config
);
925 cmd_show_run(cli
, command
, argv
, argc
);
926 cli_print_callback(cli
, NULL
);
927 fclose(save_config_fh
);
932 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
937 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
939 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
942 char ipv6addr
[INET6_ADDRSTRLEN
];
944 if (CLI_HELP_REQUESTED
)
945 return CLI_HELP_NO_ARGS
;
947 cli_print(cli
, "# Current configuration:");
949 for (i
= 0; config_values
[i
].key
; i
++)
951 void *value
= ((void *)config
) + config_values
[i
].offset
;
952 if (config_values
[i
].type
== STRING
)
953 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
954 else if (config_values
[i
].type
== IPv4
)
955 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
956 else if (config_values
[i
].type
== IPv6
)
957 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
958 else if (config_values
[i
].type
== SHORT
)
959 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
960 else if (config_values
[i
].type
== BOOL
)
961 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
962 else if (config_values
[i
].type
== INT
)
963 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
964 else if (config_values
[i
].type
== UNSIGNED_LONG
)
965 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
968 cli_print(cli
, "# Plugins");
969 for (i
= 0; i
< MAXPLUGINS
; i
++)
971 if (*config
->plugins
[i
])
973 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
978 if (config
->as_number
)
983 cli_print(cli
, "# BGP");
984 cli_print(cli
, "router bgp %u", config
->as_number
);
985 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
987 if (!config
->neighbour
[i
].name
[0])
990 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
992 k
= config
->neighbour
[i
].keepalive
;
993 h
= config
->neighbour
[i
].hold
;
1000 k
= BGP_KEEPALIVE_TIME
;
1006 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1011 cli_print(cli
, "# Filters");
1012 for (i
= 0; i
< MAXFILTER
; i
++)
1014 ip_filter_rulet
*rules
;
1015 if (!*ip_filters
[i
].name
)
1018 cli_print(cli
, "ip access-list %s %s",
1019 ip_filters
[i
].extended
? "extended" : "standard",
1020 ip_filters
[i
].name
);
1022 rules
= ip_filters
[i
].rules
;
1023 while (rules
->action
)
1024 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1027 cli_print(cli
, "# end");
1031 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1033 int i
, free
= 0, used
= 0, show_all
= 0;
1045 if (CLI_HELP_REQUESTED
)
1048 return cli_arg_help(cli
, 1, NULL
);
1050 return cli_arg_help(cli
, 1,
1051 "all", "Show all RADIUS sessions, including unused",
1055 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1059 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1062 for (i
= 1; i
< MAXRADIUS
; i
++)
1064 if (radius
[i
].state
== RADIUSNULL
)
1069 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1071 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1075 states
[radius
[i
].state
],
1081 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1086 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1090 if (CLI_HELP_REQUESTED
)
1091 return CLI_HELP_NO_ARGS
;
1093 cli_print(cli
, "Plugins currently loaded:");
1094 for (i
= 0; i
< MAXPLUGINS
; i
++)
1095 if (*config
->plugins
[i
])
1096 cli_print(cli
, " %s", config
->plugins
[i
]);
1101 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1105 if (CLI_HELP_REQUESTED
)
1106 return CLI_HELP_NO_ARGS
;
1108 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1117 for (i
= 0; i
< MAXSESSION
; i
++)
1119 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1120 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1124 session
[i
].throttle_in
,
1125 session
[i
].throttle_out
,
1127 session
[i
].tbf_out
);
1133 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1135 if (CLI_HELP_REQUESTED
)
1136 return CLI_HELP_NO_ARGS
;
1138 cli_print(cli
, " _\n"
1144 " ( \\ `. `-. _,.-:\\\n"
1145 " \\ \\ `. `-._ __..--' ,-';/\n"
1146 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1147 " `. `. `-._ __..--' ,' /\n"
1148 " `. `-_ ``--..'' _.-' ,'\n"
1149 " `-_ `-.___ __,--' ,'\n"
1150 " `-.__ `----\"\"\" __.-'\n"
1151 "hh `--..____..--'");
1156 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1161 if (CLI_HELP_REQUESTED
)
1162 return cli_arg_help(cli
, argc
> 1,
1163 "USER", "Username of session to drop", NULL
);
1165 if (!config
->cluster_iam_master
)
1167 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1168 fmtaddr(config
->cluster_master_address
, 0));
1175 cli_error(cli
, "Specify a user to drop");
1179 for (i
= 0; i
< argc
; i
++)
1181 if (!(s
= sessionbyuser(argv
[i
])))
1183 cli_error(cli
, "User %s is not connected", argv
[i
]);
1187 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1189 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1190 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1197 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1202 if (CLI_HELP_REQUESTED
)
1203 return cli_arg_help(cli
, argc
> 1,
1204 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1206 if (!config
->cluster_iam_master
)
1208 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1209 fmtaddr(config
->cluster_master_address
, 0));
1216 cli_error(cli
, "Specify a tunnel to drop");
1220 for (i
= 0; i
< argc
; i
++)
1222 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1224 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1230 cli_error(cli
, "Tunnel %d is not connected", t
);
1236 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1240 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1241 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1247 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1252 if (CLI_HELP_REQUESTED
)
1253 return cli_arg_help(cli
, argc
> 1,
1254 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1256 if (!config
->cluster_iam_master
)
1258 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1259 fmtaddr(config
->cluster_master_address
, 0));
1266 cli_error(cli
, "Specify a session id to drop");
1270 for (i
= 0; i
< argc
; i
++)
1272 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1274 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1278 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1280 cli_print(cli
, "Dropping session %d", s
);
1281 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1285 cli_error(cli
, "Session %d is not active.", s
);
1292 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1298 if (CLI_HELP_REQUESTED
)
1303 return cli_arg_help(cli
, 0,
1304 "USER", "Username of session to snoop", NULL
);
1307 return cli_arg_help(cli
, 0,
1308 "A.B.C.D", "IP address of snoop destination", NULL
);
1311 return cli_arg_help(cli
, 0,
1312 "N", "Port of snoop destination", NULL
);
1316 return cli_arg_help(cli
, 1, NULL
);
1323 if (!config
->cluster_iam_master
)
1325 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1326 fmtaddr(config
->cluster_master_address
, 0));
1333 cli_error(cli
, "Specify username, ip and port");
1337 if (!(s
= sessionbyuser(argv
[0])))
1339 cli_error(cli
, "User %s is not connected", argv
[0]);
1343 ip
= inet_addr(argv
[1]);
1344 if (!ip
|| ip
== INADDR_NONE
)
1346 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1350 port
= atoi(argv
[2]);
1353 cli_error(cli
, "Invalid port %s", argv
[2]);
1357 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1358 cli_session_actions
[s
].snoop_ip
= ip
;
1359 cli_session_actions
[s
].snoop_port
= port
;
1360 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1365 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1370 if (CLI_HELP_REQUESTED
)
1371 return cli_arg_help(cli
, argc
> 1,
1372 "USER", "Username of session to unsnoop", NULL
);
1374 if (!config
->cluster_iam_master
)
1376 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1377 fmtaddr(config
->cluster_master_address
, 0));
1384 cli_error(cli
, "Specify a user to unsnoop");
1388 for (i
= 0; i
< argc
; i
++)
1390 if (!(s
= sessionbyuser(argv
[i
])))
1392 cli_error(cli
, "User %s is not connected", argv
[i
]);
1396 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1397 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1403 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1410 throttle USER - throttle in/out to default rate
1411 throttle USER RATE - throttle in/out to default rate
1412 throttle USER in RATE - throttle input only
1413 throttle USER out RATE - throttle output only
1414 throttle USER in RATE out RATE - throttle both
1417 if (CLI_HELP_REQUESTED
)
1422 return cli_arg_help(cli
, 0,
1423 "USER", "Username of session to throttle", NULL
);
1426 return cli_arg_help(cli
, 1,
1427 "RATE", "Rate in kbps (in and out)",
1428 "in", "Select incoming rate",
1429 "out", "Select outgoing rate", NULL
);
1432 return cli_arg_help(cli
, 1,
1433 "in", "Select incoming rate",
1434 "out", "Select outgoing rate", NULL
);
1437 if (isdigit(argv
[1][0]))
1438 return cli_arg_help(cli
, 1, NULL
);
1441 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1444 return cli_arg_help(cli
, argc
> 1, NULL
);
1448 if (!config
->cluster_iam_master
)
1450 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1451 fmtaddr(config
->cluster_master_address
, 0));
1458 cli_error(cli
, "Specify a user to throttle");
1462 if (!(s
= sessionbyuser(argv
[0])))
1464 cli_error(cli
, "User %s is not connected", argv
[0]);
1470 rate_in
= rate_out
= config
->rl_rate
;
1474 rate_in
= rate_out
= atoi(argv
[1]);
1477 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1481 else if (argc
== 3 || argc
== 5)
1484 for (i
= 1; i
< argc
- 1; i
+= 2)
1487 if (MATCH("in", argv
[i
]))
1488 r
= rate_in
= atoi(argv
[i
+1]);
1489 else if (MATCH("out", argv
[i
]))
1490 r
= rate_out
= atoi(argv
[i
+1]);
1494 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1501 cli_error(cli
, "Invalid arguments");
1505 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1507 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1511 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1512 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1513 cli_session_actions
[s
].throttle_in
= rate_in
;
1515 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1516 cli_session_actions
[s
].throttle_out
= rate_out
;
1518 if (cli_session_actions
[s
].throttle_in
== -1 &&
1519 cli_session_actions
[s
].throttle_out
== -1)
1521 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1525 cli_print(cli
, "Throttling user %s", argv
[0]);
1526 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1531 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1536 if (CLI_HELP_REQUESTED
)
1537 return cli_arg_help(cli
, argc
> 1,
1538 "USER", "Username of session to unthrottle", NULL
);
1540 if (!config
->cluster_iam_master
)
1542 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1543 fmtaddr(config
->cluster_master_address
, 0));
1550 cli_error(cli
, "Specify a user to unthrottle");
1554 for (i
= 0; i
< argc
; i
++)
1556 if (!(s
= sessionbyuser(argv
[i
])))
1558 cli_error(cli
, "User %s is not connected", argv
[i
]);
1562 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1564 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1565 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1569 cli_error(cli
, "User %s not throttled", argv
[i
]);
1576 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1580 if (CLI_HELP_REQUESTED
)
1581 return cli_arg_help(cli
, 1,
1582 "all", "Enable debugging for all except \"data\"",
1583 "critical", "", // FIXME: add descriptions
1593 char *p
= (char *) &debug_flags
;
1594 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1598 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1599 (debug_flags
.critical
) ? " critical" : "",
1600 (debug_flags
.error
) ? " error" : "",
1601 (debug_flags
.warning
) ? " warning" : "",
1602 (debug_flags
.info
) ? " info" : "",
1603 (debug_flags
.calls
) ? " calls" : "",
1604 (debug_flags
.data
) ? " data" : "");
1610 cli_print(cli
, "Debugging off");
1614 for (i
= 0; i
< argc
; i
++)
1616 int len
= strlen(argv
[i
]);
1618 if (argv
[i
][0] == 'c' && len
< 2)
1619 len
= 2; /* distinguish [cr]itical from [ca]lls */
1621 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1622 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1623 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1624 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1625 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1626 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1627 if (!strncmp("all", argv
[i
], len
))
1629 memset(&debug_flags
, 1, sizeof(debug_flags
));
1630 debug_flags
.data
= 0;
1634 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1640 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1644 if (CLI_HELP_REQUESTED
)
1645 return cli_arg_help(cli
, 1,
1646 "all", "Disable all debugging",
1647 "critical", "", // FIXME: add descriptions
1657 memset(&debug_flags
, 0, sizeof(debug_flags
));
1661 for (i
= 0; i
< argc
; i
++)
1663 int len
= strlen(argv
[i
]);
1665 if (argv
[i
][0] == 'c' && len
< 2)
1666 len
= 2; /* distinguish [cr]itical from [ca]lls */
1668 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1669 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1670 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1671 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1672 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1673 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1674 if (!strncmp("all", argv
[i
], len
))
1676 memset(&debug_flags
, 0, sizeof(debug_flags
));
1680 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1686 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1688 int i
, firstfree
= 0;
1690 if (CLI_HELP_REQUESTED
)
1691 return cli_arg_help(cli
, argc
> 1,
1692 "PLUGIN", "Name of plugin to load", NULL
);
1696 cli_error(cli
, "Specify a plugin to load");
1700 for (i
= 0; i
< MAXPLUGINS
; i
++)
1702 if (!*config
->plugins
[i
] && !firstfree
)
1704 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1706 cli_error(cli
, "Plugin is already loaded");
1713 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1714 config
->reload_config
= 1;
1715 cli_print(cli
, "Loading plugin %s", argv
[0]);
1721 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1725 if (CLI_HELP_REQUESTED
)
1726 return cli_arg_help(cli
, argc
> 1,
1727 "PLUGIN", "Name of plugin to unload", NULL
);
1731 cli_error(cli
, "Specify a plugin to remove");
1735 for (i
= 0; i
< MAXPLUGINS
; i
++)
1737 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1739 config
->reload_config
= 1;
1740 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1745 cli_error(cli
, "Plugin is not loaded");
1749 static char *duration(time_t secs
)
1751 static char *buf
= NULL
;
1754 if (!buf
) buf
= calloc(64, 1);
1758 int days
= secs
/ 86400;
1759 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1765 int mins
= secs
/ 60;
1766 int hrs
= mins
/ 60;
1769 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1771 else if (secs
>= 60)
1773 int mins
= secs
/ 60;
1774 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1777 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1782 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1785 char buf
[100], *p
= buf
, *loads
[3];
1786 int i
, num_sessions
= 0;
1788 if (CLI_HELP_REQUESTED
)
1789 return CLI_HELP_NO_ARGS
;
1791 fh
= fopen("/proc/loadavg", "r");
1792 fgets(buf
, 100, fh
);
1795 for (i
= 0; i
< 3; i
++)
1796 loads
[i
] = strdup(strsep(&p
, " "));
1799 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1801 for (i
= 1; i
< MAXSESSION
; i
++)
1802 if (session
[i
].opened
) num_sessions
++;
1804 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1806 duration(time_now
- config
->start_time
),
1808 loads
[0], loads
[1], loads
[2]
1810 for (i
= 0; i
< 3; i
++)
1811 if (loads
[i
]) free(loads
[i
]);
1813 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1818 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1822 if (CLI_HELP_REQUESTED
)
1828 int len
= strlen(argv
[0])-1;
1829 for (i
= 0; config_values
[i
].key
; i
++)
1830 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1831 cli_error(cli
, " %s", config_values
[i
].key
);
1837 return cli_arg_help(cli
, 0,
1838 "VALUE", "Value for variable", NULL
);
1842 return cli_arg_help(cli
, 1, NULL
);
1851 cli_error(cli
, "Specify variable and value");
1855 for (i
= 0; config_values
[i
].key
; i
++)
1857 void *value
= ((void *) config
) + config_values
[i
].offset
;
1858 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1860 // Found a value to set
1861 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1862 switch (config_values
[i
].type
)
1865 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1868 *(int *) value
= atoi(argv
[1]);
1871 *(unsigned long *) value
= atol(argv
[1]);
1874 *(short *) value
= atoi(argv
[1]);
1877 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1880 inet_pton(AF_INET6
, argv
[1], value
);
1883 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1889 cli_error(cli
, "Unknown variable type");
1892 config
->reload_config
= 1;
1897 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1901 int regular_stuff(struct cli_def
*cli
)
1907 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1909 char *m
= ringbuffer
->buffer
[i
].message
;
1915 switch (ringbuffer
->buffer
[i
].level
)
1917 case 0: show
= debug_flags
.critical
; break;
1918 case 1: show
= debug_flags
.error
; break;
1919 case 2: show
= debug_flags
.warning
; break;
1920 case 3: show
= debug_flags
.info
; break;
1921 case 4: show
= debug_flags
.calls
; break;
1922 case 5: show
= debug_flags
.data
; break;
1925 if (!show
) continue;
1927 if (!(p
= strchr(m
, '\n')))
1930 cli_error(cli
, "\r%s-%u-%u %.*s",
1931 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1932 ringbuffer
->buffer
[i
].tunnel
,
1933 ringbuffer
->buffer
[i
].session
,
1939 debug_rb_tail
= ringbuffer
->tail
;
1947 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1951 if (CLI_HELP_REQUESTED
)
1952 return cli_arg_help(cli
, argc
> 1,
1953 "<1-65535>", "Autonomous system number", NULL
);
1955 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1957 cli_error(cli
, "Invalid autonomous system number");
1961 if (bgp_configured
&& as
!= config
->as_number
)
1963 cli_error(cli
, "Can't change local AS on a running system");
1967 config
->as_number
= as
;
1968 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1973 static int find_bgp_neighbour(char const *name
)
1978 in_addr_t addrs
[4] = { 0 };
1981 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1984 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1985 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1987 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1989 if (!config
->neighbour
[i
].name
[0])
1991 if (new == -1) new = i
;
1995 if (!strcmp(name
, config
->neighbour
[i
].name
))
1998 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
2001 for (a
= h
->h_addr_list
; *a
; a
++)
2004 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
2005 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2013 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2019 if (CLI_HELP_REQUESTED
)
2024 return cli_arg_help(cli
, 0,
2025 "A.B.C.D", "BGP neighbour address",
2026 "NAME", "BGP neighbour name",
2030 return cli_arg_help(cli
, 0,
2031 "remote-as", "Set remote autonomous system number",
2032 "timers", "Set timers",
2036 if (MATCH("remote-as", argv
[1]))
2037 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2039 if (MATCH("timers", argv
[1]))
2042 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2045 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2047 if (argc
== 5 && !argv
[4][1])
2048 return cli_arg_help(cli
, 1, NULL
);
2057 cli_error(cli
, "Invalid arguments");
2061 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2063 cli_error(cli
, "Invalid neighbour");
2069 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2073 if (MATCH("remote-as", argv
[1]))
2075 int as
= atoi(argv
[2]);
2076 if (as
< 0 || as
> 65535)
2078 cli_error(cli
, "Invalid autonomous system number");
2082 if (!config
->neighbour
[i
].name
[0])
2084 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2085 config
->neighbour
[i
].keepalive
= -1;
2086 config
->neighbour
[i
].hold
= -1;
2089 config
->neighbour
[i
].as
= as
;
2093 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2095 cli_error(cli
, "Invalid arguments");
2099 if (!config
->neighbour
[i
].name
[0])
2101 cli_error(cli
, "Specify remote-as first");
2105 keepalive
= atoi(argv
[2]);
2106 hold
= atoi(argv
[3]);
2108 if (keepalive
< 1 || keepalive
> 65535)
2110 cli_error(cli
, "Invalid keepalive time");
2114 if (hold
< 3 || hold
> 65535)
2116 cli_error(cli
, "Invalid hold time");
2120 if (keepalive
== BGP_KEEPALIVE_TIME
)
2121 keepalive
= -1; // using default value
2123 if (hold
== BGP_HOLD_TIME
)
2126 config
->neighbour
[i
].keepalive
= keepalive
;
2127 config
->neighbour
[i
].hold
= hold
;
2132 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2136 if (CLI_HELP_REQUESTED
)
2137 return cli_arg_help(cli
, argc
> 0,
2138 "A.B.C.D", "BGP neighbour address",
2139 "NAME", "BGP neighbour name",
2144 cli_error(cli
, "Specify a BGP neighbour");
2148 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2150 cli_error(cli
, "Invalid neighbour");
2154 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2156 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2160 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2164 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2170 if (!bgp_configured
)
2173 if (CLI_HELP_REQUESTED
)
2174 return cli_arg_help(cli
, 1,
2175 "A.B.C.D", "BGP neighbour address",
2176 "NAME", "BGP neighbour name",
2179 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2180 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2184 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2186 if (!*bgp_peers
[i
].name
)
2189 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2190 if (argc
&& strcmp(addr
, argv
[0]) &&
2191 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2197 cli_print(cli
, "Peer AS Address "
2198 "State Retries Retry in Route Pend Timers");
2199 cli_print(cli
, "------------------ ----- --------------- "
2200 "----------- ------- -------- ----- ---- ---------");
2203 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2207 bgp_state_str(bgp_peers
[i
].state
),
2208 bgp_peers
[i
].retry_count
,
2209 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2210 bgp_peers
[i
].routing
? "yes" : "no",
2211 bgp_peers
[i
].update_routes
? "yes" : "no",
2212 bgp_peers
[i
].keepalive
,
2219 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2224 if (!bgp_configured
)
2227 if (CLI_HELP_REQUESTED
)
2228 return cli_arg_help(cli
, 1,
2229 "A.B.C.D", "BGP neighbour address",
2230 "NAME", "BGP neighbour name",
2233 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2235 if (bgp_peers
[i
].state
!= Established
)
2238 if (!bgp_peers
[i
].routing
)
2241 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2242 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2245 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2246 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2252 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2257 if (!bgp_configured
)
2260 if (CLI_HELP_REQUESTED
)
2261 return cli_arg_help(cli
, 1,
2262 "A.B.C.D", "BGP neighbour address",
2263 "NAME", "BGP neighbour name",
2266 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2268 if (bgp_peers
[i
].state
!= Established
)
2271 if (bgp_peers
[i
].routing
)
2274 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2275 if (argc
&& strcmp(addr
, argv
[0]) &&
2276 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2279 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2280 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2286 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2291 if (!bgp_configured
)
2294 if (CLI_HELP_REQUESTED
)
2295 return cli_arg_help(cli
, 1,
2296 "A.B.C.D", "BGP neighbour address",
2297 "NAME", "BGP neighbour name",
2300 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2302 if (!*bgp_peers
[i
].name
)
2305 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2306 if (argc
&& strcmp(addr
, argv
[0]) &&
2307 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2310 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2311 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2319 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2323 if (CLI_HELP_REQUESTED
)
2328 return cli_arg_help(cli
, 0,
2329 "standard", "Standard syntax",
2330 "extended", "Extended syntax",
2334 return cli_arg_help(cli
, argv
[1][1],
2335 "NAME", "Access-list name",
2339 if (argc
== 3 && !argv
[2][1])
2340 return cli_arg_help(cli
, 1, NULL
);
2348 cli_error(cli
, "Specify access-list type and name");
2352 if (MATCH("standard", argv
[0]))
2354 else if (MATCH("extended", argv
[0]))
2358 cli_error(cli
, "Invalid access-list type");
2362 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2363 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2365 cli_error(cli
, "Invalid access-list name");
2369 filt
= find_filter(argv
[1], strlen(argv
[1]));
2374 cli_error(cli
, "Too many access-lists");
2379 if (!*ip_filters
[filt
].name
)
2381 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2382 strcpy(ip_filters
[filt
].name
, argv
[1]);
2383 ip_filters
[filt
].extended
= extended
;
2385 else if (ip_filters
[filt
].extended
!= extended
)
2387 cli_error(cli
, "Access-list is %s",
2388 ip_filters
[filt
].extended
? "extended" : "standard");
2393 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2397 if (filt
< 0 || !*ip_filters
[filt
].name
)
2399 cli_error(cli
, "Access-list not defined");
2404 if (ip_filters
[filt
].used
)
2406 cli_error(cli
, "Access-list in use");
2410 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2414 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2416 return access_list(cli
, argv
, argc
, 1);
2419 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2421 return access_list(cli
, argv
, argc
, 0);
2424 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2426 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2427 return sprintf(buf
, " any");
2429 if (wild
== INADDR_ANY
)
2430 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2432 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2435 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2439 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2440 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2441 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2442 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2443 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2449 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2451 static char buf
[256];
2454 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2457 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2458 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2461 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2465 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2466 p
+= show_ports(p
, &rule
->src_ports
);
2468 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2469 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2470 p
+= show_ports(p
, &rule
->dst_ports
);
2472 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2474 switch (rule
->tcp_flag_op
)
2476 case FILTER_FLAG_OP_EST
:
2477 p
+= sprintf(p
, " established");
2480 case FILTER_FLAG_OP_ANY
:
2481 case FILTER_FLAG_OP_ALL
:
2482 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2483 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2484 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2485 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2486 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2487 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2488 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2489 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2490 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2491 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2492 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2493 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2494 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2500 p
+= sprintf(p
, " fragments");
2505 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2507 static ip_filter_rulet rule
;
2508 struct in_addr addr
;
2512 if (CLI_HELP_REQUESTED
)
2516 cli_arg_help(cli
, 0,
2517 "ip", "Match IP packets",
2518 "tcp", "Match TCP packets",
2519 "udp", "Match UDP packets",
2525 // *sigh*, too darned complex
2526 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2532 cli_error(cli
, "Specify rule details");
2536 memset(&rule
, 0, sizeof(rule
));
2537 rule
.action
= (command
[0] == 'p')
2538 ? FILTER_ACTION_PERMIT
2539 : FILTER_ACTION_DENY
;
2541 if (MATCH("ip", argv
[0]))
2542 rule
.proto
= IPPROTO_IP
;
2543 else if (MATCH("udp", argv
[0]))
2544 rule
.proto
= IPPROTO_UDP
;
2545 else if (MATCH("tcp", argv
[0]))
2546 rule
.proto
= IPPROTO_TCP
;
2549 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2553 for (a
= 1, i
= 0; i
< 2; i
++)
2557 ip_filter_portt
*port
;
2562 wild
= &rule
.src_wild
;
2563 port
= &rule
.src_ports
;
2568 wild
= &rule
.dst_wild
;
2569 port
= &rule
.dst_ports
;
2572 cli_error(cli
, "Specify destination");
2577 if (MATCH("any", argv
[a
]))
2580 *wild
= INADDR_BROADCAST
;
2583 else if (MATCH("host", argv
[a
]))
2587 cli_error(cli
, "Specify host ip address");
2591 if (!inet_aton(argv
[a
], &addr
))
2593 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2605 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2609 if (!inet_aton(argv
[a
], &addr
))
2611 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2617 if (!inet_aton(argv
[++a
], &addr
))
2619 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2623 *wild
= addr
.s_addr
;
2627 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2631 if (MATCH("eq", argv
[a
]))
2632 port
->op
= FILTER_PORT_OP_EQ
;
2633 else if (MATCH("neq", argv
[a
]))
2634 port
->op
= FILTER_PORT_OP_NEQ
;
2635 else if (MATCH("gt", argv
[a
]))
2636 port
->op
= FILTER_PORT_OP_GT
;
2637 else if (MATCH("lt", argv
[a
]))
2638 port
->op
= FILTER_PORT_OP_LT
;
2639 else if (MATCH("range", argv
[a
]))
2640 port
->op
= FILTER_PORT_OP_RANGE
;
2647 cli_error(cli
, "Specify port");
2651 if (!(port
->port
= atoi(argv
[a
])))
2653 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2658 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2663 cli_error(cli
, "Specify port");
2667 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2669 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2676 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2678 if (MATCH("established", argv
[a
]))
2680 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2683 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2684 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2686 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2687 ? FILTER_FLAG_OP_ANY
2688 : FILTER_FLAG_OP_ALL
;
2692 cli_error(cli
, "Specify tcp flags");
2696 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2700 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2702 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2703 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2704 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2705 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2706 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2707 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2710 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2719 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2721 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2723 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2733 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2740 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2742 static ip_filter_rulet rule
;
2743 struct in_addr addr
;
2745 if (CLI_HELP_REQUESTED
)
2749 cli_arg_help(cli
, argv
[0][1],
2750 "A.B.C.D", "Source address",
2751 "any", "Any source address",
2752 "host", "Source host",
2758 if (MATCH("any", argv
[0]))
2760 if (argc
== 2 && !argv
[1][1])
2761 cli_arg_help(cli
, 1, NULL
);
2763 else if (MATCH("host", argv
[0]))
2767 cli_arg_help(cli
, argv
[1][1],
2768 "A.B.C.D", "Host address",
2771 else if (argc
== 3 && !argv
[2][1])
2772 cli_arg_help(cli
, 1, NULL
);
2778 cli_arg_help(cli
, 1,
2779 "A.B.C.D", "Wildcard bits",
2782 else if (argc
== 3 && !argv
[2][1])
2783 cli_arg_help(cli
, 1, NULL
);
2791 cli_error(cli
, "Specify rule details");
2795 memset(&rule
, 0, sizeof(rule
));
2796 rule
.action
= (command
[0] == 'p')
2797 ? FILTER_ACTION_PERMIT
2798 : FILTER_ACTION_DENY
;
2800 rule
.proto
= IPPROTO_IP
;
2801 if (MATCH("any", argv
[0]))
2803 rule
.src_ip
= INADDR_ANY
;
2804 rule
.src_wild
= INADDR_BROADCAST
;
2806 else if (MATCH("host", argv
[0]))
2810 cli_error(cli
, "Specify host ip address");
2814 if (!inet_aton(argv
[1], &addr
))
2816 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2820 rule
.src_ip
= addr
.s_addr
;
2821 rule
.src_wild
= INADDR_ANY
;
2827 cli_error(cli
, "Specify source ip address and wildcard");
2831 if (!inet_aton(argv
[0], &addr
))
2833 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2837 rule
.src_ip
= addr
.s_addr
;
2841 if (!inet_aton(argv
[1], &addr
))
2843 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2847 rule
.src_wild
= addr
.s_addr
;
2850 rule
.src_wild
= INADDR_ANY
;
2856 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2859 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2860 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2861 : access_list_rule_std(cli
, command
, argv
, argc
);
2866 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2868 if (!ip_filters
[filt
].rules
[i
].action
)
2870 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2874 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2878 cli_error(cli
, "Too many rules");
2882 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2887 /* filter USER {in|out} FILTER ... */
2888 if (CLI_HELP_REQUESTED
)
2893 return cli_arg_help(cli
, 0,
2894 "USER", "Username of session to filter", NULL
);
2898 return cli_arg_help(cli
, 0,
2899 "in", "Set incoming filter",
2900 "out", "Set outgoing filter", NULL
);
2904 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2905 "NAME", "Filter name", NULL
);
2908 return cli_arg_help(cli
, argc
> 1, NULL
);
2912 if (!config
->cluster_iam_master
)
2914 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2915 fmtaddr(config
->cluster_master_address
, 0));
2920 if (argc
!= 3 && argc
!= 5)
2922 cli_error(cli
, "Specify a user and filters");
2926 if (!(s
= sessionbyuser(argv
[0])))
2928 cli_error(cli
, "User %s is not connected", argv
[0]);
2932 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2933 for (i
= 1; i
< argc
; i
+= 2)
2938 if (MATCH("in", argv
[i
]))
2940 if (session
[s
].filter_in
)
2942 cli_error(cli
, "Input already filtered");
2945 f
= &cli_session_actions
[s
].filter_in
;
2947 else if (MATCH("out", argv
[i
]))
2949 if (session
[s
].filter_out
)
2951 cli_error(cli
, "Output already filtered");
2954 f
= &cli_session_actions
[s
].filter_out
;
2958 cli_error(cli
, "Invalid filter specification");
2962 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
2963 if (v
< 0 || !*ip_filters
[v
].name
)
2965 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
2972 cli_print(cli
, "Filtering user %s", argv
[0]);
2973 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2978 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2983 if (CLI_HELP_REQUESTED
)
2984 return cli_arg_help(cli
, argc
> 1,
2985 "USER", "Username of session to remove filters from", NULL
);
2987 if (!config
->cluster_iam_master
)
2989 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2990 fmtaddr(config
->cluster_master_address
, 0));
2997 cli_error(cli
, "Specify a user to remove filters from");
3001 for (i
= 0; i
< argc
; i
++)
3003 if (!(s
= sessionbyuser(argv
[i
])))
3005 cli_error(cli
, "User %s is not connected", argv
[i
]);
3009 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3011 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3012 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3016 cli_error(cli
, "User %s not filtered", argv
[i
]);
3023 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3027 if (CLI_HELP_REQUESTED
)
3028 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3032 cli_error(cli
, "Specify a filter name");
3036 for (i
= 0; i
< argc
; i
++)
3038 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
3039 ip_filter_rulet
*rules
;
3041 if (f
< 0 || !*ip_filters
[f
].name
)
3043 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3050 cli_print(cli
, "%s IP access list %s",
3051 ip_filters
[f
].extended
? "Extended" : "Standard",
3052 ip_filters
[f
].name
);
3054 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3056 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3058 cli_print(cli
, "%s (%u match%s)", r
,
3059 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3061 cli_print(cli
, "%s", r
);