1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.43 2004/12/17 00:28:00 bodea Exp $";
18 #include <arpa/inet.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
36 extern tunnelt
*tunnel
;
37 extern sessiont
*session
;
38 extern radiust
*radius
;
39 extern ippoolt
*ip_address_pool
;
40 extern struct Tstats
*_statistics
;
41 static struct cli_def
*cli
= NULL
;
42 extern configt
*config
;
43 extern config_descriptt config_values
[];
45 extern struct Tringbuffer
*ringbuffer
;
47 extern struct cli_session_actions
*cli_session_actions
;
48 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
49 extern tbft
*filter_list
;
50 extern ip_filtert
*ip_filters
;
52 static char *debug_levels
[] = {
71 static int debug_session
;
72 static int debug_tunnel
;
73 static int debug_rb_tail
;
75 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 static int regular_stuff(struct cli_def
*cli
);
102 static void parsemac(char *string
, char mac
[6]);
105 #define MODE_CONFIG_BGP 8
106 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
107 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
110 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
111 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
112 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
115 #define MODE_CONFIG_NACL 9
116 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
120 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
121 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
123 /* match if b is a substr of a */
124 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
126 void init_cli(char *hostname
)
130 struct cli_command
*c
;
131 struct cli_command
*c2
;
133 struct sockaddr_in addr
;
136 if (hostname
&& *hostname
)
137 cli_set_hostname(cli
, hostname
);
139 cli_set_hostname(cli
, "l2tpns");
141 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
142 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
144 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
146 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
147 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
148 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
149 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
150 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
151 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
152 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
153 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
154 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
155 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
156 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");
157 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
158 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
160 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
161 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
162 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
165 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
167 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
168 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
171 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
173 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
174 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
175 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
177 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
178 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
179 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
180 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
183 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
184 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
187 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
188 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
189 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
190 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
191 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
194 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
195 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
197 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
198 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
200 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
201 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
203 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
205 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
206 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
209 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
210 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
211 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
212 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
214 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
215 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
217 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
218 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
220 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
222 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
223 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
225 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
226 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
228 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
229 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
230 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
232 // Enable regular processing
233 cli_regular(cli
, regular_stuff
);
235 if (!(f
= fopen(CLIUSERS
, "r")))
237 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
241 while (fgets(buf
, 4096, f
))
244 if (*buf
== '#') continue;
245 if ((p
= strchr(buf
, '\r'))) *p
= 0;
246 if ((p
= strchr(buf
, '\n'))) *p
= 0;
248 if (!(p
= strchr((char *)buf
, ':'))) continue;
250 if (!strcmp(buf
, "enable"))
252 cli_allow_enable(cli
, p
);
253 LOG(3, 0, 0, "Setting enable password\n");
257 cli_allow_user(cli
, buf
, p
);
258 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
264 memset(&addr
, 0, sizeof(addr
));
265 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
266 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
269 // Set cli fd as non-blocking
270 flags
= fcntl(clifd
, F_GETFL
, 0);
271 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
273 addr
.sin_family
= AF_INET
;
274 addr
.sin_port
= htons(23);
275 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
277 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
283 void cli_do(int sockfd
)
285 int require_auth
= 1;
286 struct sockaddr_in addr
;
287 int l
= sizeof(addr
);
289 if (fork_and_close()) return;
290 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
292 LOG(3, 0, 0, "Accepted connection to CLI from %s\n", fmtaddr(addr
.sin_addr
.s_addr
, 0));
293 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
296 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
300 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
301 if (!cli
->users
) /* paranoia */
303 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
309 /* no username/pass required */
316 debug_rb_tail
= ringbuffer
->tail
;
318 memset(&debug_flags
, 0, sizeof(debug_flags
));
319 debug_flags
.critical
= 1;
321 cli_loop(cli
, sockfd
);
324 LOG(3, 0, 0, "Closed CLI connection from %s\n", fmtaddr(addr
.sin_addr
.s_addr
, 0));
328 static void cli_print_log(struct cli_def
*cli
, char *string
)
330 LOG(3, 0, 0, "%s\n", string
);
333 void cli_do_file(FILE *fh
)
335 LOG(3, 0, 0, "Reading configuration file\n");
336 cli_print_callback(cli
, cli_print_log
);
337 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
338 cli_print_callback(cli
, NULL
);
341 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
352 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
354 int v
= va_arg(ap
, int);
355 snprintf(buf
, sizeof(buf
), entry
, v
);
361 desc
= va_arg(ap
, char *);
363 cli_print(cli
, " %-20s %s", p
, desc
);
365 cli_print(cli
, " %s", p
);
367 entry
= desc
? va_arg(ap
, char *) : 0;
372 cli_print(cli
, " <cr>");
377 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
381 if (CLI_HELP_REQUESTED
)
382 return cli_arg_help(cli
, 1,
383 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
389 // Show individual session
390 for (i
= 0; i
< argc
; i
++)
392 unsigned int s
, b_in
, b_out
;
394 if (s
<= 0 || s
>= MAXSESSION
)
396 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
399 cli_print(cli
, "\r\nSession %d:", s
);
400 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
401 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
402 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
403 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
404 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
405 cli_print(cli
, "\tUnique SID:\t%lu", session
[s
].unique_id
);
406 cli_print(cli
, "\tIdle time:\t%u seconds", abs(time_now
- session
[s
].last_packet
));
407 cli_print(cli
, "\tNext Recv:\t%u", session
[s
].nr
);
408 cli_print(cli
, "\tNext Send:\t%u", session
[s
].ns
);
409 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].total_cout
, session
[s
].total_cin
);
410 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
411 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
412 cli_print(cli
, "\tRadius Session:\t%u", session
[s
].radius
);
413 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
414 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
415 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
416 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
417 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
418 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
419 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
420 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
422 cli_print(cli
, "\tIntercepted:\tno");
424 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
426 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
427 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
428 t
? "YES" : "no", t
? " (" : "",
429 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
431 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
435 b_in
= session
[s
].tbf_in
;
436 b_out
= session
[s
].tbf_out
;
438 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
439 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
440 "ByteSent", "PackSent", "PackDrop", "PackDelay");
443 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
445 (filter_list
[b_in
].next
? "*" : " "),
446 (b_in
< 100 ? "\t" : ""),
447 filter_list
[b_in
].rate
* 8,
448 filter_list
[b_in
].credit
,
449 filter_list
[b_in
].queued
,
450 filter_list
[b_in
].b_queued
,
451 filter_list
[b_in
].p_queued
,
452 filter_list
[b_in
].b_sent
,
453 filter_list
[b_in
].p_sent
,
454 filter_list
[b_in
].p_dropped
,
455 filter_list
[b_in
].p_delayed
);
458 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
460 (filter_list
[b_out
].next
? "*" : " "),
461 (b_out
< 100 ? "\t" : ""),
462 filter_list
[b_out
].rate
* 8,
463 filter_list
[b_out
].credit
,
464 filter_list
[b_out
].queued
,
465 filter_list
[b_out
].b_queued
,
466 filter_list
[b_out
].p_queued
,
467 filter_list
[b_out
].b_sent
,
468 filter_list
[b_out
].p_sent
,
469 filter_list
[b_out
].p_dropped
,
470 filter_list
[b_out
].p_delayed
);
477 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
492 for (i
= 1; i
< MAXSESSION
; i
++)
494 if (!session
[i
].opened
) continue;
495 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
498 session
[i
].user
[0] ? session
[i
].user
: "*",
499 fmtaddr(htonl(session
[i
].ip
), 0),
500 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
501 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
502 (session
[i
].walled_garden
) ? "Y" : "N",
503 abs(time_now
- (unsigned long)session
[i
].opened
),
504 (unsigned long)session
[i
].total_cout
,
505 (unsigned long)session
[i
].total_cin
,
506 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
507 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
508 session
[i
].calling
[0] ? session
[i
].calling
: "*");
513 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
515 int i
, x
, show_all
= 0;
523 if (CLI_HELP_REQUESTED
)
526 return cli_arg_help(cli
, 1,
527 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
530 return cli_arg_help(cli
, 1,
531 "all", "Show all tunnels, including unused",
532 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
539 if (strcmp(argv
[0], "all") == 0)
545 // Show individual tunnel
546 for (i
= 0; i
< argc
; i
++)
551 if (t
<= 0 || t
>= MAXTUNNEL
)
553 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
556 cli_print(cli
, "\r\nTunnel %d:", t
);
557 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
558 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
559 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
560 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
561 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
562 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
563 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
564 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
565 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
567 for (x
= 0; x
< MAXSESSION
; x
++)
568 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
569 sprintf(s
, "%s%u ", s
, x
);
571 cli_print(cli
, "\tSessions:\t%s", s
);
577 // Show tunnel summary
578 cli_print(cli
, "%4s %20s %20s %6s %s",
585 for (i
= 1; i
< MAXTUNNEL
; i
++)
588 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
590 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
591 cli_print(cli
, "%4d %20s %20s %6s %6d",
593 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
594 fmtaddr(htonl(tunnel
[i
].ip
), 0),
595 states
[tunnel
[i
].state
],
602 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
609 if (CLI_HELP_REQUESTED
)
610 return cli_arg_help(cli
, 1,
611 "USER", "Show details for specific username",
614 for (i
= 0; i
< MAXSESSION
; i
++)
616 if (!session
[i
].opened
) continue;
617 if (!session
[i
].user
[0]) continue;
621 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
623 if (strcmp(argv
[j
], session
[i
].user
) == 0)
625 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
626 sargv
[sargc
] = sid
[sargc
];
634 cli_print(cli
, "%s", session
[i
].user
);
638 return cmd_show_session(cli
, "users", sargv
, sargc
);
643 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
645 if (CLI_HELP_REQUESTED
)
646 return CLI_HELP_NO_ARGS
;
648 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
649 cli_print(cli
, "%-10s %8u %8u %8u", "RX",
650 GET_STAT(tun_rx_bytes
),
651 GET_STAT(tun_rx_packets
),
652 GET_STAT(tun_rx_errors
));
653 cli_print(cli
, "%-10s %8u %8u %8u", "TX",
654 GET_STAT(tun_tx_bytes
),
655 GET_STAT(tun_tx_packets
),
656 GET_STAT(tun_tx_errors
));
659 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
660 cli_print(cli
, "%-10s %8u %8u %8u", "RX",
661 GET_STAT(tunnel_rx_bytes
),
662 GET_STAT(tunnel_rx_packets
),
663 GET_STAT(tunnel_rx_errors
));
664 cli_print(cli
, "%-10s %8u %8u %8u %8u", "TX",
665 GET_STAT(tunnel_tx_bytes
),
666 GET_STAT(tunnel_tx_packets
),
667 GET_STAT(tunnel_tx_errors
),
668 GET_STAT(tunnel_retries
));
671 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
672 cli_print(cli
, "-----------------------------------------");
673 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
674 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
675 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
676 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
677 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
678 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
679 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
680 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
681 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
682 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
683 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
684 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
685 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
686 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
687 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
688 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
689 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
690 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
694 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
695 cli_print(cli
, "-----------------------------------------");
696 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
697 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
698 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
699 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
700 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
701 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
702 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
703 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
704 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
705 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
706 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
707 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
708 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
709 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
710 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
711 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
712 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
713 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
714 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
715 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
716 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
717 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
718 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
719 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
720 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
721 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
726 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
732 if (CLI_HELP_REQUESTED
)
733 return cli_arg_help(cli
, 1,
734 "tag", "Include CVS release tag",
735 "file", "Include file versions",
738 for (i
= 0; i
< argc
; i
++)
739 if (!strcmp(argv
[i
], "tag"))
741 else if (!strcmp(argv
[i
], "file"))
744 cli_print(cli
, "L2TPNS %s", VERSION
);
747 char const *p
= strchr(cvs_name
, ':');
759 e
= strpbrk(p
, " \t$");
760 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
765 extern linked_list
*loaded_plugins
;
768 cli_print(cli
, "Files:");
769 cli_print(cli
, " %s", cvs_id_arp
);
771 cli_print(cli
, " %s", cvs_id_bgp
);
773 cli_print(cli
, " %s", cvs_id_cli
);
774 cli_print(cli
, " %s", cvs_id_cluster
);
775 cli_print(cli
, " %s", cvs_id_constants
);
776 cli_print(cli
, " %s", cvs_id_control
);
777 cli_print(cli
, " %s", cvs_id_icmp
);
778 cli_print(cli
, " %s", cvs_id_l2tpns
);
779 cli_print(cli
, " %s", cvs_id_ll
);
780 cli_print(cli
, " %s", cvs_id_md5
);
781 cli_print(cli
, " %s", cvs_id_ppp
);
782 cli_print(cli
, " %s", cvs_id_radius
);
783 cli_print(cli
, " %s", cvs_id_tbf
);
784 cli_print(cli
, " %s", cvs_id_util
);
786 ll_reset(loaded_plugins
);
787 while ((p
= ll_next(loaded_plugins
)))
789 char const **id
= dlsym(p
, "cvs_id");
791 cli_print(cli
, " %s", *id
);
798 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
801 int used
= 0, free
= 0, show_all
= 0;
803 if (!config
->cluster_iam_master
)
805 cli_print(cli
, "Can't do this on a slave. Do it on %s",
806 fmtaddr(config
->cluster_master_address
, 0));
811 if (CLI_HELP_REQUESTED
)
814 return cli_arg_help(cli
, 1, NULL
);
816 return cli_arg_help(cli
, 1,
817 "all", "Show all pool addresses, including unused",
821 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
825 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
826 for (i
= 0; i
< MAXIPPOOL
; i
++)
828 if (!ip_address_pool
[i
].address
) continue;
829 if (ip_address_pool
[i
].assigned
)
831 cli_print(cli
, "%-15s\tY %8d %s",
832 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
833 ip_address_pool
[i
].session
,
834 session
[ip_address_pool
[i
].session
].user
);
840 if (ip_address_pool
[i
].last
)
841 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
842 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
843 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
846 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
853 cli_print(cli
, "(Not displaying unused addresses)");
855 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
859 static FILE *save_config_fh
= 0;
860 static void print_save_config(struct cli_def
*cli
, char *string
)
863 fprintf(save_config_fh
, "%s\n", string
);
866 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
868 if (CLI_HELP_REQUESTED
)
869 return CLI_HELP_NO_ARGS
;
871 if ((save_config_fh
= fopen(config
->config_file
, "w")))
873 cli_print(cli
, "Writing configuration");
874 cli_print_callback(cli
, print_save_config
);
875 cmd_show_run(cli
, command
, argv
, argc
);
876 cli_print_callback(cli
, NULL
);
877 fclose(save_config_fh
);
882 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
887 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
889 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
893 if (CLI_HELP_REQUESTED
)
894 return CLI_HELP_NO_ARGS
;
896 cli_print(cli
, "# Current configuration:");
898 for (i
= 0; config_values
[i
].key
; i
++)
900 void *value
= ((void *)config
) + config_values
[i
].offset
;
901 if (config_values
[i
].type
== STRING
)
902 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
903 else if (config_values
[i
].type
== IP
)
904 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(unsigned *)value
, 0));
905 else if (config_values
[i
].type
== SHORT
)
906 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
907 else if (config_values
[i
].type
== BOOL
)
908 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
909 else if (config_values
[i
].type
== INT
)
910 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
911 else if (config_values
[i
].type
== UNSIGNED_LONG
)
912 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
913 else if (config_values
[i
].type
== MAC
)
914 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
915 *(unsigned short *)(value
+ 0),
916 *(unsigned short *)(value
+ 1),
917 *(unsigned short *)(value
+ 2),
918 *(unsigned short *)(value
+ 3),
919 *(unsigned short *)(value
+ 4),
920 *(unsigned short *)(value
+ 5));
923 cli_print(cli
, "# Plugins");
924 for (i
= 0; i
< MAXPLUGINS
; i
++)
926 if (*config
->plugins
[i
])
928 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
933 if (config
->as_number
)
938 cli_print(cli
, "# BGP");
939 cli_print(cli
, "router bgp %u", config
->as_number
);
940 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
942 if (!config
->neighbour
[i
].name
[0])
945 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
947 k
= config
->neighbour
[i
].keepalive
;
948 h
= config
->neighbour
[i
].hold
;
955 k
= BGP_KEEPALIVE_TIME
;
961 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
966 cli_print(cli
, "# Filters");
967 for (i
= 0; i
< MAXFILTER
; i
++)
969 ip_filter_rulet
*rules
;
970 if (!*ip_filters
[i
].name
)
973 cli_print(cli
, "ip access-list %s %s",
974 ip_filters
[i
].extended
? "extended" : "standard",
977 rules
= ip_filters
[i
].rules
;
978 while (rules
->action
)
979 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
982 cli_print(cli
, "# end");
986 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
988 int i
, free
= 0, used
= 0, show_all
= 0;
999 if (CLI_HELP_REQUESTED
)
1002 return cli_arg_help(cli
, 1, NULL
);
1004 return cli_arg_help(cli
, 1,
1005 "all", "Show all RADIUS sessions, including unused",
1009 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1013 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1016 for (i
= 1; i
< MAXRADIUS
; i
++)
1018 if (radius
[i
].state
== RADIUSNULL
)
1023 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1025 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1029 states
[radius
[i
].state
],
1035 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1040 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1044 if (CLI_HELP_REQUESTED
)
1045 return CLI_HELP_NO_ARGS
;
1047 cli_print(cli
, "Plugins currently loaded:");
1048 for (i
= 0; i
< MAXPLUGINS
; i
++)
1049 if (*config
->plugins
[i
])
1050 cli_print(cli
, " %s", config
->plugins
[i
]);
1055 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1059 if (CLI_HELP_REQUESTED
)
1060 return CLI_HELP_NO_ARGS
;
1062 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1071 for (i
= 0; i
< MAXSESSION
; i
++)
1073 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1074 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1078 session
[i
].throttle_in
,
1079 session
[i
].throttle_out
,
1081 session
[i
].tbf_out
);
1087 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1089 if (CLI_HELP_REQUESTED
)
1090 return CLI_HELP_NO_ARGS
;
1092 cli_print(cli
, " _\n"
1098 " ( \\ `. `-. _,.-:\\\n"
1099 " \\ \\ `. `-._ __..--' ,-';/\n"
1100 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1101 " `. `. `-._ __..--' ,' /\n"
1102 " `. `-_ ``--..'' _.-' ,'\n"
1103 " `-_ `-.___ __,--' ,'\n"
1104 " `-.__ `----\"\"\" __.-'\n"
1105 "hh `--..____..--'");
1110 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1112 if (CLI_HELP_REQUESTED
)
1113 return CLI_HELP_NO_ARGS
;
1115 cli_print(cli
, "Counters cleared");
1116 SET_STAT(last_reset
, time(NULL
));
1120 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1125 if (CLI_HELP_REQUESTED
)
1126 return cli_arg_help(cli
, argc
> 1,
1127 "USER", "Username of session to drop", NULL
);
1129 if (!config
->cluster_iam_master
)
1131 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1132 fmtaddr(config
->cluster_master_address
, 0));
1139 cli_print(cli
, "Specify a user to drop");
1143 for (i
= 0; i
< argc
; i
++)
1145 if (!(s
= sessionbyuser(argv
[i
])))
1147 cli_print(cli
, "User %s is not connected", argv
[i
]);
1151 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1153 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1154 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1161 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1166 if (CLI_HELP_REQUESTED
)
1167 return cli_arg_help(cli
, argc
> 1,
1168 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1170 if (!config
->cluster_iam_master
)
1172 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1173 fmtaddr(config
->cluster_master_address
, 0));
1180 cli_print(cli
, "Specify a tunnel to drop");
1184 for (i
= 0; i
< argc
; i
++)
1186 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1188 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1194 cli_print(cli
, "Tunnel %d is not connected", t
);
1200 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1204 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1205 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1211 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1216 if (CLI_HELP_REQUESTED
)
1217 return cli_arg_help(cli
, argc
> 1,
1218 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1220 if (!config
->cluster_iam_master
)
1222 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1223 fmtaddr(config
->cluster_master_address
, 0));
1230 cli_print(cli
, "Specify a session id to drop");
1234 for (i
= 0; i
< argc
; i
++)
1236 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1238 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1242 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1244 cli_print(cli
, "Dropping session %d", s
);
1245 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1249 cli_print(cli
, "Session %d is not active.", s
);
1256 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1262 if (CLI_HELP_REQUESTED
)
1267 return cli_arg_help(cli
, 0,
1268 "USER", "Username of session to snoop", NULL
);
1271 return cli_arg_help(cli
, 0,
1272 "A.B.C.D", "IP address of snoop destination", NULL
);
1275 return cli_arg_help(cli
, 0,
1276 "N", "Port of snoop destination", NULL
);
1280 return cli_arg_help(cli
, 1, NULL
);
1287 if (!config
->cluster_iam_master
)
1289 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1290 fmtaddr(config
->cluster_master_address
, 0));
1297 cli_print(cli
, "Specify username, ip and port");
1301 if (!(s
= sessionbyuser(argv
[0])))
1303 cli_print(cli
, "User %s is not connected", argv
[0]);
1307 ip
= inet_addr(argv
[1]);
1308 if (!ip
|| ip
== INADDR_NONE
)
1310 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1314 port
= atoi(argv
[2]);
1317 cli_print(cli
, "Invalid port %s", argv
[2]);
1321 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1322 cli_session_actions
[s
].snoop_ip
= ip
;
1323 cli_session_actions
[s
].snoop_port
= port
;
1324 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1329 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1334 if (CLI_HELP_REQUESTED
)
1335 return cli_arg_help(cli
, argc
> 1,
1336 "USER", "Username of session to unsnoop", NULL
);
1338 if (!config
->cluster_iam_master
)
1340 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1341 fmtaddr(config
->cluster_master_address
, 0));
1348 cli_print(cli
, "Specify a user to unsnoop");
1352 for (i
= 0; i
< argc
; i
++)
1354 if (!(s
= sessionbyuser(argv
[i
])))
1356 cli_print(cli
, "User %s is not connected", argv
[i
]);
1360 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1361 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1367 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1374 throttle USER - throttle in/out to default rate
1375 throttle USER RATE - throttle in/out to default rate
1376 throttle USER in RATE - throttle input only
1377 throttle USER out RATE - throttle output only
1378 throttle USER in RATE out RATE - throttle both
1381 if (CLI_HELP_REQUESTED
)
1386 return cli_arg_help(cli
, 0,
1387 "USER", "Username of session to throttle", NULL
);
1390 return cli_arg_help(cli
, 1,
1391 "RATE", "Rate in kbps (in and out)",
1392 "in", "Select incoming rate",
1393 "out", "Select outgoing rate", NULL
);
1396 return cli_arg_help(cli
, 1,
1397 "in", "Select incoming rate",
1398 "out", "Select outgoing rate", NULL
);
1401 if (isdigit(argv
[1][0]))
1402 return cli_arg_help(cli
, 1, NULL
);
1405 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1408 return cli_arg_help(cli
, argc
> 1, NULL
);
1412 if (!config
->cluster_iam_master
)
1414 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1415 fmtaddr(config
->cluster_master_address
, 0));
1422 cli_print(cli
, "Specify a user to throttle");
1426 if (!(s
= sessionbyuser(argv
[0])))
1428 cli_print(cli
, "User %s is not connected", argv
[0]);
1434 rate_in
= rate_out
= config
->rl_rate
;
1438 rate_in
= rate_out
= atoi(argv
[1]);
1441 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1445 else if (argc
== 3 || argc
== 5)
1448 for (i
= 1; i
< argc
- 1; i
+= 2)
1451 if (MATCH("in", argv
[i
]))
1452 r
= rate_in
= atoi(argv
[i
+1]);
1453 else if (MATCH("out", argv
[i
]))
1454 r
= rate_out
= atoi(argv
[i
+1]);
1458 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1465 cli_print(cli
, "Invalid arguments");
1469 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1471 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1475 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1476 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1477 cli_session_actions
[s
].throttle_in
= rate_in
;
1479 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1480 cli_session_actions
[s
].throttle_out
= rate_out
;
1482 if (cli_session_actions
[s
].throttle_in
== -1 &&
1483 cli_session_actions
[s
].throttle_out
== -1)
1485 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1489 cli_print(cli
, "Throttling user %s", argv
[0]);
1490 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1495 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1500 if (CLI_HELP_REQUESTED
)
1501 return cli_arg_help(cli
, argc
> 1,
1502 "USER", "Username of session to unthrottle", NULL
);
1504 if (!config
->cluster_iam_master
)
1506 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1507 fmtaddr(config
->cluster_master_address
, 0));
1514 cli_print(cli
, "Specify a user to unthrottle");
1518 for (i
= 0; i
< argc
; i
++)
1520 if (!(s
= sessionbyuser(argv
[i
])))
1522 cli_print(cli
, "User %s is not connected", argv
[i
]);
1526 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1528 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1529 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1533 cli_print(cli
, "User %s not throttled", argv
[i
]);
1540 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1544 if (CLI_HELP_REQUESTED
)
1545 return cli_arg_help(cli
, 1,
1546 "all", "Enable debugging for all except \"data\"",
1547 "critical", "", // FIXME: add descriptions
1557 char *p
= (char *) &debug_flags
;
1558 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1562 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1563 (debug_flags
.critical
) ? " critical" : "",
1564 (debug_flags
.error
) ? " error" : "",
1565 (debug_flags
.warning
) ? " warning" : "",
1566 (debug_flags
.info
) ? " info" : "",
1567 (debug_flags
.calls
) ? " calls" : "",
1568 (debug_flags
.data
) ? " data" : "");
1574 cli_print(cli
, "Debugging off");
1578 for (i
= 0; i
< argc
; i
++)
1580 int len
= strlen(argv
[i
]);
1582 if (argv
[i
][0] == 'c' && len
< 2)
1583 len
= 2; /* distinguish [cr]itical from [ca]lls */
1585 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1586 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1587 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1588 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1589 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1590 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1591 if (!strncmp("all", argv
[i
], len
))
1593 memset(&debug_flags
, 1, sizeof(debug_flags
));
1594 debug_flags
.data
= 0;
1598 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1604 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1608 if (CLI_HELP_REQUESTED
)
1609 return cli_arg_help(cli
, 1,
1610 "all", "Disable all debugging",
1611 "critical", "", // FIXME: add descriptions
1621 memset(&debug_flags
, 0, sizeof(debug_flags
));
1625 for (i
= 0; i
< argc
; i
++)
1627 int len
= strlen(argv
[i
]);
1629 if (argv
[i
][0] == 'c' && len
< 2)
1630 len
= 2; /* distinguish [cr]itical from [ca]lls */
1632 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1633 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1634 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1635 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1636 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1637 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1638 if (!strncmp("all", argv
[i
], len
))
1640 memset(&debug_flags
, 0, sizeof(debug_flags
));
1644 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1650 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1652 int i
, firstfree
= 0;
1654 if (CLI_HELP_REQUESTED
)
1655 return cli_arg_help(cli
, argc
> 1,
1656 "PLUGIN", "Name of plugin to load", NULL
);
1660 cli_print(cli
, "Specify a plugin to load");
1664 for (i
= 0; i
< MAXPLUGINS
; i
++)
1666 if (!*config
->plugins
[i
] && !firstfree
)
1668 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1670 cli_print(cli
, "Plugin is already loaded");
1677 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1678 config
->reload_config
= 1;
1679 cli_print(cli
, "Loading plugin %s", argv
[0]);
1685 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1689 if (CLI_HELP_REQUESTED
)
1690 return cli_arg_help(cli
, argc
> 1,
1691 "PLUGIN", "Name of plugin to unload", NULL
);
1695 cli_print(cli
, "Specify a plugin to remove");
1699 for (i
= 0; i
< MAXPLUGINS
; i
++)
1701 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1703 config
->reload_config
= 1;
1704 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1709 cli_print(cli
, "Plugin is not loaded");
1713 static char *duration(time_t secs
)
1715 static char *buf
= NULL
;
1718 if (!buf
) buf
= calloc(64, 1);
1722 int days
= secs
/ 86400;
1723 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1729 int mins
= secs
/ 60;
1730 int hrs
= mins
/ 60;
1733 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1735 else if (secs
>= 60)
1737 int mins
= secs
/ 60;
1738 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1741 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1746 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1749 char buf
[100], *p
= buf
, *loads
[3];
1750 int i
, num_sessions
= 0;
1752 if (CLI_HELP_REQUESTED
)
1753 return CLI_HELP_NO_ARGS
;
1755 fh
= fopen("/proc/loadavg", "r");
1756 fgets(buf
, 100, fh
);
1759 for (i
= 0; i
< 3; i
++)
1760 loads
[i
] = strdup(strsep(&p
, " "));
1763 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1765 for (i
= 1; i
< MAXSESSION
; i
++)
1766 if (session
[i
].opened
) num_sessions
++;
1768 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1770 duration(time_now
- config
->start_time
),
1772 loads
[0], loads
[1], loads
[2]
1774 for (i
= 0; i
< 3; i
++)
1775 if (loads
[i
]) free(loads
[i
]);
1777 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1782 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1786 if (CLI_HELP_REQUESTED
)
1792 int len
= strlen(argv
[0])-1;
1793 for (i
= 0; config_values
[i
].key
; i
++)
1794 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1795 cli_print(cli
, " %s", config_values
[i
].key
);
1801 return cli_arg_help(cli
, 0,
1802 "VALUE", "Value for variable", NULL
);
1806 return cli_arg_help(cli
, 1, NULL
);
1815 cli_print(cli
, "Specify variable and value");
1819 for (i
= 0; config_values
[i
].key
; i
++)
1821 void *value
= ((void *)config
) + config_values
[i
].offset
;
1822 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1824 // Found a value to set
1825 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1826 switch (config_values
[i
].type
)
1829 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1832 *(int *)value
= atoi(argv
[1]);
1835 *(unsigned long *)value
= atol(argv
[1]);
1838 *(short *)value
= atoi(argv
[1]);
1841 *(unsigned *)value
= inet_addr(argv
[1]);
1844 parsemac(argv
[1], (char *)value
);
1847 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1853 cli_print(cli
, "Unknown variable type");
1856 config
->reload_config
= 1;
1861 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1865 int regular_stuff(struct cli_def
*cli
)
1871 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1873 char *m
= ringbuffer
->buffer
[i
].message
;
1879 switch (ringbuffer
->buffer
[i
].level
)
1881 case 0: show
= debug_flags
.critical
; break;
1882 case 1: show
= debug_flags
.error
; break;
1883 case 2: show
= debug_flags
.warning
; break;
1884 case 3: show
= debug_flags
.info
; break;
1885 case 4: show
= debug_flags
.calls
; break;
1886 case 5: show
= debug_flags
.data
; break;
1889 if (!show
) continue;
1891 if (!(p
= strchr(m
, '\n')))
1894 cli_print(cli
, "\r%s-%u-%u %.*s",
1895 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1896 ringbuffer
->buffer
[i
].tunnel
,
1897 ringbuffer
->buffer
[i
].session
,
1903 debug_rb_tail
= ringbuffer
->tail
;
1911 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1915 if (CLI_HELP_REQUESTED
)
1916 return cli_arg_help(cli
, argc
> 1,
1917 "<1-65535>", "Autonomous system number", NULL
);
1919 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1921 cli_print(cli
, "Invalid autonomous system number");
1925 if (bgp_configured
&& as
!= config
->as_number
)
1927 cli_print(cli
, "Can't change local AS on a running system");
1931 config
->as_number
= as
;
1932 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1937 static int find_bgp_neighbour(char const *name
)
1942 in_addr_t addrs
[4] = { 0 };
1945 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1948 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1949 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1951 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1953 if (!config
->neighbour
[i
].name
[0])
1955 if (new == -1) new = i
;
1959 if (!strcmp(name
, config
->neighbour
[i
].name
))
1962 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1965 for (a
= h
->h_addr_list
; *a
; a
++)
1968 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1969 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1977 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1983 if (CLI_HELP_REQUESTED
)
1988 return cli_arg_help(cli
, 0,
1989 "A.B.C.D", "BGP neighbour address",
1990 "NAME", "BGP neighbour name",
1994 return cli_arg_help(cli
, 0,
1995 "remote-as", "Set remote autonomous system number",
1996 "timers", "Set timers",
2000 if (MATCH("remote-as", argv
[1]))
2001 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2003 if (MATCH("timers", argv
[1]))
2006 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2009 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2011 if (argc
== 5 && !argv
[4][1])
2012 return cli_arg_help(cli
, 1, NULL
);
2021 cli_print(cli
, "Invalid arguments");
2025 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2027 cli_print(cli
, "Invalid neighbour");
2033 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2037 if (MATCH("remote-as", argv
[1]))
2039 int as
= atoi(argv
[2]);
2040 if (as
< 0 || as
> 65535)
2042 cli_print(cli
, "Invalid autonomous system number");
2046 if (!config
->neighbour
[i
].name
[0])
2048 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2049 config
->neighbour
[i
].keepalive
= -1;
2050 config
->neighbour
[i
].hold
= -1;
2053 config
->neighbour
[i
].as
= as
;
2057 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2059 cli_print(cli
, "Invalid arguments");
2063 if (!config
->neighbour
[i
].name
[0])
2065 cli_print(cli
, "Specify remote-as first");
2069 keepalive
= atoi(argv
[2]);
2070 hold
= atoi(argv
[3]);
2072 if (keepalive
< 1 || keepalive
> 65535)
2074 cli_print(cli
, "Invalid keepalive time");
2078 if (hold
< 3 || hold
> 65535)
2080 cli_print(cli
, "Invalid hold time");
2084 if (keepalive
== BGP_KEEPALIVE_TIME
)
2085 keepalive
= -1; // using default value
2087 if (hold
== BGP_HOLD_TIME
)
2090 config
->neighbour
[i
].keepalive
= keepalive
;
2091 config
->neighbour
[i
].hold
= hold
;
2096 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2100 if (CLI_HELP_REQUESTED
)
2101 return cli_arg_help(cli
, argc
> 0,
2102 "A.B.C.D", "BGP neighbour address",
2103 "NAME", "BGP neighbour name",
2108 cli_print(cli
, "Specify a BGP neighbour");
2112 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2114 cli_print(cli
, "Invalid neighbour");
2118 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2120 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2124 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2128 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2134 if (!bgp_configured
)
2137 if (CLI_HELP_REQUESTED
)
2138 return cli_arg_help(cli
, 1,
2139 "A.B.C.D", "BGP neighbour address",
2140 "NAME", "BGP neighbour name",
2143 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2144 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2148 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2150 if (!*bgp_peers
[i
].name
)
2153 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2154 if (argc
&& strcmp(addr
, argv
[0]) &&
2155 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2161 cli_print(cli
, "Peer AS Address "
2162 "State Retries Retry in Route Pend Timers");
2163 cli_print(cli
, "------------------ ----- --------------- "
2164 "----------- ------- -------- ----- ---- ---------");
2167 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2171 bgp_state_str(bgp_peers
[i
].state
),
2172 bgp_peers
[i
].retry_count
,
2173 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2174 bgp_peers
[i
].routing
? "yes" : "no",
2175 bgp_peers
[i
].update_routes
? "yes" : "no",
2176 bgp_peers
[i
].keepalive
,
2183 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2188 if (!bgp_configured
)
2191 if (CLI_HELP_REQUESTED
)
2192 return cli_arg_help(cli
, 1,
2193 "A.B.C.D", "BGP neighbour address",
2194 "NAME", "BGP neighbour name",
2197 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2199 if (bgp_peers
[i
].state
!= Established
)
2202 if (!bgp_peers
[i
].routing
)
2205 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2206 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2209 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2210 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2216 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2221 if (!bgp_configured
)
2224 if (CLI_HELP_REQUESTED
)
2225 return cli_arg_help(cli
, 1,
2226 "A.B.C.D", "BGP neighbour address",
2227 "NAME", "BGP neighbour name",
2230 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2232 if (bgp_peers
[i
].state
!= Established
)
2235 if (bgp_peers
[i
].routing
)
2238 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2239 if (argc
&& strcmp(addr
, argv
[0]) &&
2240 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2243 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2244 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2250 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2255 if (!bgp_configured
)
2258 if (CLI_HELP_REQUESTED
)
2259 return cli_arg_help(cli
, 1,
2260 "A.B.C.D", "BGP neighbour address",
2261 "NAME", "BGP neighbour name",
2264 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2266 if (!*bgp_peers
[i
].name
)
2269 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2270 if (argc
&& strcmp(addr
, argv
[0]) &&
2271 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2274 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2275 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2283 static int find_access_list(char const *name
)
2287 for (i
= 0; i
< MAXFILTER
; i
++)
2288 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2294 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2298 if (CLI_HELP_REQUESTED
)
2303 return cli_arg_help(cli
, 0,
2304 "standard", "Standard syntax",
2305 "extended", "Extended syntax",
2309 return cli_arg_help(cli
, argv
[1][1],
2310 "NAME", "Access-list name",
2314 if (argc
== 3 && !argv
[2][1])
2315 return cli_arg_help(cli
, 1, NULL
);
2323 cli_print(cli
, "Specify access-list type and name");
2327 if (MATCH("standard", argv
[0]))
2329 else if (MATCH("extended", argv
[0]))
2333 cli_print(cli
, "Invalid access-list type");
2337 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2338 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2340 cli_print(cli
, "Invalid access-list name");
2344 filt
= find_access_list(argv
[1]);
2349 cli_print(cli
, "Too many access-lists");
2354 if (!*ip_filters
[filt
].name
)
2356 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2357 strcpy(ip_filters
[filt
].name
, argv
[1]);
2358 ip_filters
[filt
].extended
= extended
;
2360 else if (ip_filters
[filt
].extended
!= extended
)
2362 cli_print(cli
, "Access-list is %s",
2363 ip_filters
[filt
].extended
? "extended" : "standard");
2368 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2372 if (filt
< 0 || !*ip_filters
[filt
].name
)
2374 cli_print(cli
, "Access-list not defined");
2379 if (ip_filters
[filt
].used
)
2381 cli_print(cli
, "Access-list in use");
2385 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2389 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2391 return access_list(cli
, argv
, argc
, 1);
2394 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2396 return access_list(cli
, argv
, argc
, 0);
2399 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2401 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2402 return sprintf(buf
, " any");
2404 if (wild
== INADDR_ANY
)
2405 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2407 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2410 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2414 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2415 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2416 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2417 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2418 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2424 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2426 static char buf
[256];
2429 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2432 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2433 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2436 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2440 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2441 p
+= show_ports(p
, &rule
->src_ports
);
2443 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2444 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2445 p
+= show_ports(p
, &rule
->dst_ports
);
2447 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2449 switch (rule
->tcp_flag_op
)
2451 case FILTER_FLAG_OP_EST
:
2452 p
+= sprintf(p
, " established");
2455 case FILTER_FLAG_OP_ANY
:
2456 case FILTER_FLAG_OP_ALL
:
2457 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2458 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2459 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2460 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2461 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2462 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2463 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2464 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2465 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2466 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2467 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2468 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2469 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2475 p
+= sprintf(p
, " fragments");
2480 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2482 static ip_filter_rulet rule
;
2483 struct in_addr addr
;
2487 if (CLI_HELP_REQUESTED
)
2491 cli_arg_help(cli
, 0,
2492 "ip", "Match IP packets",
2493 "tcp", "Match TCP packets",
2494 "udp", "Match UDP packets",
2500 // *sigh*, too darned complex
2501 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2507 cli_print(cli
, "Specify rule details");
2511 memset(&rule
, 0, sizeof(rule
));
2512 rule
.action
= (command
[0] == 'p')
2513 ? FILTER_ACTION_PERMIT
2514 : FILTER_ACTION_DENY
;
2516 if (MATCH("ip", argv
[0]))
2517 rule
.proto
= IPPROTO_IP
;
2518 else if (MATCH("udp", argv
[0]))
2519 rule
.proto
= IPPROTO_UDP
;
2520 else if (MATCH("tcp", argv
[0]))
2521 rule
.proto
= IPPROTO_TCP
;
2524 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2528 for (a
= 1, i
= 0; i
< 2; i
++)
2532 ip_filter_portt
*port
;
2537 wild
= &rule
.src_wild
;
2538 port
= &rule
.src_ports
;
2543 wild
= &rule
.dst_wild
;
2544 port
= &rule
.dst_ports
;
2547 cli_print(cli
, "Specify destination");
2552 if (MATCH("any", argv
[a
]))
2555 *wild
= INADDR_BROADCAST
;
2558 else if (MATCH("host", argv
[a
]))
2562 cli_print(cli
, "Specify host ip address");
2566 if (!inet_aton(argv
[a
], &addr
))
2568 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2580 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2584 if (!inet_aton(argv
[a
], &addr
))
2586 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2592 if (!inet_aton(argv
[++a
], &addr
))
2594 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2598 *wild
= addr
.s_addr
;
2602 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2606 if (MATCH("eq", argv
[a
]))
2607 port
->op
= FILTER_PORT_OP_EQ
;
2608 else if (MATCH("neq", argv
[a
]))
2609 port
->op
= FILTER_PORT_OP_NEQ
;
2610 else if (MATCH("gt", argv
[a
]))
2611 port
->op
= FILTER_PORT_OP_GT
;
2612 else if (MATCH("lt", argv
[a
]))
2613 port
->op
= FILTER_PORT_OP_LT
;
2614 else if (MATCH("range", argv
[a
]))
2615 port
->op
= FILTER_PORT_OP_RANGE
;
2622 cli_print(cli
, "Specify port");
2626 if (!(port
->port
= atoi(argv
[a
])))
2628 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2633 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2638 cli_print(cli
, "Specify port");
2642 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2644 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2651 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2653 if (MATCH("established", argv
[a
]))
2655 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2658 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2659 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2661 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2662 ? FILTER_FLAG_OP_ANY
2663 : FILTER_FLAG_OP_ALL
;
2667 cli_print(cli
, "Specify tcp flags");
2671 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2675 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2677 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2678 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2679 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2680 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2681 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2682 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2685 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2694 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2696 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2698 cli_print(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2708 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2715 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2717 static ip_filter_rulet rule
;
2718 struct in_addr addr
;
2720 if (CLI_HELP_REQUESTED
)
2724 cli_arg_help(cli
, argv
[0][1],
2725 "A.B.C.D", "Source address",
2726 "any", "Any source address",
2727 "host", "Source host",
2733 if (MATCH("any", argv
[0]))
2735 if (argc
== 2 && !argv
[1][1])
2736 cli_arg_help(cli
, 1, NULL
);
2738 else if (MATCH("host", argv
[0]))
2742 cli_arg_help(cli
, argv
[1][1],
2743 "A.B.C.D", "Host address",
2746 else if (argc
== 3 && !argv
[2][1])
2747 cli_arg_help(cli
, 1, NULL
);
2753 cli_arg_help(cli
, 1,
2754 "A.B.C.D", "Wildcard bits",
2757 else if (argc
== 3 && !argv
[2][1])
2758 cli_arg_help(cli
, 1, NULL
);
2766 cli_print(cli
, "Specify rule details");
2770 memset(&rule
, 0, sizeof(rule
));
2771 rule
.action
= (command
[0] == 'p')
2772 ? FILTER_ACTION_PERMIT
2773 : FILTER_ACTION_DENY
;
2775 rule
.proto
= IPPROTO_IP
;
2776 if (MATCH("any", argv
[0]))
2778 rule
.src_ip
= INADDR_ANY
;
2779 rule
.src_wild
= INADDR_BROADCAST
;
2781 else if (MATCH("host", argv
[0]))
2785 cli_print(cli
, "Specify host ip address");
2789 if (!inet_aton(argv
[1], &addr
))
2791 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2795 rule
.src_ip
= addr
.s_addr
;
2796 rule
.src_wild
= INADDR_ANY
;
2802 cli_print(cli
, "Specify source ip address and wildcard");
2806 if (!inet_aton(argv
[0], &addr
))
2808 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2812 rule
.src_ip
= addr
.s_addr
;
2816 if (!inet_aton(argv
[1], &addr
))
2818 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2822 rule
.src_wild
= addr
.s_addr
;
2825 rule
.src_wild
= INADDR_ANY
;
2831 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2834 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2835 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2836 : access_list_rule_std(cli
, command
, argv
, argc
);
2841 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2843 if (!ip_filters
[filt
].rules
[i
].action
)
2845 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2849 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2853 cli_print(cli
, "Too many rules");
2857 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2862 /* filter USER {in|out} FILTER ... */
2863 if (CLI_HELP_REQUESTED
)
2868 return cli_arg_help(cli
, 0,
2869 "USER", "Username of session to filter", NULL
);
2873 return cli_arg_help(cli
, 0,
2874 "in", "Set incoming filter",
2875 "out", "Set outgoing filter", NULL
);
2879 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2880 "NAME", "Filter name", NULL
);
2883 return cli_arg_help(cli
, argc
> 1, NULL
);
2887 if (!config
->cluster_iam_master
)
2889 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2890 fmtaddr(config
->cluster_master_address
, 0));
2895 if (argc
!= 3 && argc
!= 5)
2897 cli_print(cli
, "Specify a user and filters");
2901 if (!(s
= sessionbyuser(argv
[0])))
2903 cli_print(cli
, "User %s is not connected", argv
[0]);
2907 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2908 for (i
= 1; i
< argc
; i
+= 2)
2913 if (MATCH("in", argv
[i
]))
2915 if (session
[s
].filter_in
)
2917 cli_print(cli
, "Input already filtered");
2920 f
= &cli_session_actions
[s
].filter_in
;
2922 else if (MATCH("out", argv
[i
]))
2924 if (session
[s
].filter_out
)
2926 cli_print(cli
, "Output already filtered");
2929 f
= &cli_session_actions
[s
].filter_out
;
2933 cli_print(cli
, "Invalid filter specification");
2937 v
= find_access_list(argv
[i
+1]);
2938 if (v
< 0 || !*ip_filters
[v
].name
)
2940 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2947 cli_print(cli
, "Filtering user %s", argv
[0]);
2948 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2953 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2958 if (CLI_HELP_REQUESTED
)
2959 return cli_arg_help(cli
, argc
> 1,
2960 "USER", "Username of session to remove filters from", NULL
);
2962 if (!config
->cluster_iam_master
)
2964 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2965 fmtaddr(config
->cluster_master_address
, 0));
2972 cli_print(cli
, "Specify a user to remove filters from");
2976 for (i
= 0; i
< argc
; i
++)
2978 if (!(s
= sessionbyuser(argv
[i
])))
2980 cli_print(cli
, "User %s is not connected", argv
[i
]);
2984 if (session
[s
].filter_in
|| session
[s
].filter_out
)
2986 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
2987 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
2991 cli_print(cli
, "User %s not filtered", argv
[i
]);
2998 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3002 if (CLI_HELP_REQUESTED
)
3003 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3007 cli_print(cli
, "Specify a filter name");
3011 for (i
= 0; i
< argc
; i
++)
3013 int f
= find_access_list(argv
[i
]);
3014 ip_filter_rulet
*rules
;
3016 if (f
< 0 || !*ip_filters
[f
].name
)
3018 cli_print(cli
, "Access-list %s not defined", argv
[i
]);
3025 cli_print(cli
, "%s IP access list %s",
3026 ip_filters
[f
].extended
? "Extended" : "Standard",
3027 ip_filters
[f
].name
);
3029 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3031 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3033 cli_print(cli
, "%s (%d match%s)", r
,
3034 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3036 cli_print(cli
, "%s", r
);
3043 // Convert a string in the form of abcd.ef12.3456 into char[6]
3044 void parsemac(char *string
, char mac
[6])
3046 if (sscanf(string
, "%02x%02x.%02x%02x.%02x%02x", (unsigned int *)&mac
[0], (unsigned int *)&mac
[1], (unsigned int *)&mac
[2], (unsigned int *)&mac
[3], (unsigned int *)&mac
[4], (unsigned int *)&mac
[5]) == 6)
3048 if (sscanf(string
, "%02x%02x:%02x%02x:%02x%02x", (unsigned int *)&mac
[0], (unsigned int *)&mac
[1], (unsigned int *)&mac
[2], (unsigned int *)&mac
[3], (unsigned int *)&mac
[4], (unsigned int *)&mac
[5]) == 6)