1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.43.2.2 2005/01/12 05:20:49 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 %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
649 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
650 GET_STAT(tun_rx_bytes
),
651 GET_STAT(tun_rx_packets
),
652 GET_STAT(tun_rx_errors
),
653 GET_STAT(tun_rx_dropped
));
654 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
655 GET_STAT(tun_tx_bytes
),
656 GET_STAT(tun_tx_packets
),
657 GET_STAT(tun_tx_errors
));
660 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
661 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
662 GET_STAT(tunnel_rx_bytes
),
663 GET_STAT(tunnel_rx_packets
),
664 GET_STAT(tunnel_rx_errors
));
665 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
666 GET_STAT(tunnel_tx_bytes
),
667 GET_STAT(tunnel_tx_packets
),
668 GET_STAT(tunnel_tx_errors
),
669 GET_STAT(tunnel_retries
));
672 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
673 cli_print(cli
, "-----------------------------------------");
674 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
675 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
676 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
677 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
678 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
679 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
680 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
681 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
682 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
683 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
684 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
685 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
686 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
687 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
688 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
689 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
690 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
691 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
695 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
696 cli_print(cli
, "-----------------------------------------");
697 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
698 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
699 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
700 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
701 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
702 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
703 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
704 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
705 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
706 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
707 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
708 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
709 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
710 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
711 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
712 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
713 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
714 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
715 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
716 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
717 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
718 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
719 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
720 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
721 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
722 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
727 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
733 if (CLI_HELP_REQUESTED
)
734 return cli_arg_help(cli
, 1,
735 "tag", "Include CVS release tag",
736 "file", "Include file versions",
739 for (i
= 0; i
< argc
; i
++)
740 if (!strcmp(argv
[i
], "tag"))
742 else if (!strcmp(argv
[i
], "file"))
745 cli_print(cli
, "L2TPNS %s", VERSION
);
748 char const *p
= strchr(cvs_name
, ':');
760 e
= strpbrk(p
, " \t$");
761 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
766 extern linked_list
*loaded_plugins
;
769 cli_print(cli
, "Files:");
770 cli_print(cli
, " %s", cvs_id_arp
);
772 cli_print(cli
, " %s", cvs_id_bgp
);
774 cli_print(cli
, " %s", cvs_id_cli
);
775 cli_print(cli
, " %s", cvs_id_cluster
);
776 cli_print(cli
, " %s", cvs_id_constants
);
777 cli_print(cli
, " %s", cvs_id_control
);
778 cli_print(cli
, " %s", cvs_id_icmp
);
779 cli_print(cli
, " %s", cvs_id_l2tpns
);
780 cli_print(cli
, " %s", cvs_id_ll
);
781 cli_print(cli
, " %s", cvs_id_md5
);
782 cli_print(cli
, " %s", cvs_id_ppp
);
783 cli_print(cli
, " %s", cvs_id_radius
);
784 cli_print(cli
, " %s", cvs_id_tbf
);
785 cli_print(cli
, " %s", cvs_id_util
);
787 ll_reset(loaded_plugins
);
788 while ((p
= ll_next(loaded_plugins
)))
790 char const **id
= dlsym(p
, "cvs_id");
792 cli_print(cli
, " %s", *id
);
799 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
802 int used
= 0, free
= 0, show_all
= 0;
804 if (!config
->cluster_iam_master
)
806 cli_print(cli
, "Can't do this on a slave. Do it on %s",
807 fmtaddr(config
->cluster_master_address
, 0));
812 if (CLI_HELP_REQUESTED
)
815 return cli_arg_help(cli
, 1, NULL
);
817 return cli_arg_help(cli
, 1,
818 "all", "Show all pool addresses, including unused",
822 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
826 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
827 for (i
= 0; i
< MAXIPPOOL
; i
++)
829 if (!ip_address_pool
[i
].address
) continue;
830 if (ip_address_pool
[i
].assigned
)
832 cli_print(cli
, "%-15s\tY %8d %s",
833 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
834 ip_address_pool
[i
].session
,
835 session
[ip_address_pool
[i
].session
].user
);
841 if (ip_address_pool
[i
].last
)
842 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
843 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
844 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
847 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
854 cli_print(cli
, "(Not displaying unused addresses)");
856 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
860 static FILE *save_config_fh
= 0;
861 static void print_save_config(struct cli_def
*cli
, char *string
)
864 fprintf(save_config_fh
, "%s\n", string
);
867 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
869 if (CLI_HELP_REQUESTED
)
870 return CLI_HELP_NO_ARGS
;
872 if ((save_config_fh
= fopen(config
->config_file
, "w")))
874 cli_print(cli
, "Writing configuration");
875 cli_print_callback(cli
, print_save_config
);
876 cmd_show_run(cli
, command
, argv
, argc
);
877 cli_print_callback(cli
, NULL
);
878 fclose(save_config_fh
);
883 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
888 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
890 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
894 if (CLI_HELP_REQUESTED
)
895 return CLI_HELP_NO_ARGS
;
897 cli_print(cli
, "# Current configuration:");
899 for (i
= 0; config_values
[i
].key
; i
++)
901 void *value
= ((void *)config
) + config_values
[i
].offset
;
902 if (config_values
[i
].type
== STRING
)
903 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
904 else if (config_values
[i
].type
== IP
)
905 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(unsigned *)value
, 0));
906 else if (config_values
[i
].type
== SHORT
)
907 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
908 else if (config_values
[i
].type
== BOOL
)
909 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
910 else if (config_values
[i
].type
== INT
)
911 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
912 else if (config_values
[i
].type
== UNSIGNED_LONG
)
913 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
914 else if (config_values
[i
].type
== MAC
)
915 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
916 *(unsigned short *)(value
+ 0),
917 *(unsigned short *)(value
+ 1),
918 *(unsigned short *)(value
+ 2),
919 *(unsigned short *)(value
+ 3),
920 *(unsigned short *)(value
+ 4),
921 *(unsigned short *)(value
+ 5));
924 cli_print(cli
, "# Plugins");
925 for (i
= 0; i
< MAXPLUGINS
; i
++)
927 if (*config
->plugins
[i
])
929 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
934 if (config
->as_number
)
939 cli_print(cli
, "# BGP");
940 cli_print(cli
, "router bgp %u", config
->as_number
);
941 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
943 if (!config
->neighbour
[i
].name
[0])
946 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
948 k
= config
->neighbour
[i
].keepalive
;
949 h
= config
->neighbour
[i
].hold
;
956 k
= BGP_KEEPALIVE_TIME
;
962 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
967 cli_print(cli
, "# Filters");
968 for (i
= 0; i
< MAXFILTER
; i
++)
970 ip_filter_rulet
*rules
;
971 if (!*ip_filters
[i
].name
)
974 cli_print(cli
, "ip access-list %s %s",
975 ip_filters
[i
].extended
? "extended" : "standard",
978 rules
= ip_filters
[i
].rules
;
979 while (rules
->action
)
980 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
983 cli_print(cli
, "# end");
987 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
989 int i
, free
= 0, used
= 0, show_all
= 0;
1000 if (CLI_HELP_REQUESTED
)
1003 return cli_arg_help(cli
, 1, NULL
);
1005 return cli_arg_help(cli
, 1,
1006 "all", "Show all RADIUS sessions, including unused",
1010 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1014 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1017 for (i
= 1; i
< MAXRADIUS
; i
++)
1019 if (radius
[i
].state
== RADIUSNULL
)
1024 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1026 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1030 states
[radius
[i
].state
],
1036 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1041 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1045 if (CLI_HELP_REQUESTED
)
1046 return CLI_HELP_NO_ARGS
;
1048 cli_print(cli
, "Plugins currently loaded:");
1049 for (i
= 0; i
< MAXPLUGINS
; i
++)
1050 if (*config
->plugins
[i
])
1051 cli_print(cli
, " %s", config
->plugins
[i
]);
1056 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1060 if (CLI_HELP_REQUESTED
)
1061 return CLI_HELP_NO_ARGS
;
1063 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1072 for (i
= 0; i
< MAXSESSION
; i
++)
1074 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1075 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1079 session
[i
].throttle_in
,
1080 session
[i
].throttle_out
,
1082 session
[i
].tbf_out
);
1088 static int cmd_show_banana(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
, " _\n"
1099 " ( \\ `. `-. _,.-:\\\n"
1100 " \\ \\ `. `-._ __..--' ,-';/\n"
1101 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1102 " `. `. `-._ __..--' ,' /\n"
1103 " `. `-_ ``--..'' _.-' ,'\n"
1104 " `-_ `-.___ __,--' ,'\n"
1105 " `-.__ `----\"\"\" __.-'\n"
1106 "hh `--..____..--'");
1111 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1113 if (CLI_HELP_REQUESTED
)
1114 return CLI_HELP_NO_ARGS
;
1116 memset(_statistics
, 0, sizeof(struct Tstats
));
1117 SET_STAT(last_reset
, time(NULL
));
1119 cli_print(cli
, "Counters cleared");
1123 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1128 if (CLI_HELP_REQUESTED
)
1129 return cli_arg_help(cli
, argc
> 1,
1130 "USER", "Username of session to drop", NULL
);
1132 if (!config
->cluster_iam_master
)
1134 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1135 fmtaddr(config
->cluster_master_address
, 0));
1142 cli_print(cli
, "Specify a user to drop");
1146 for (i
= 0; i
< argc
; i
++)
1148 if (!(s
= sessionbyuser(argv
[i
])))
1150 cli_print(cli
, "User %s is not connected", argv
[i
]);
1154 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1156 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1157 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1164 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1169 if (CLI_HELP_REQUESTED
)
1170 return cli_arg_help(cli
, argc
> 1,
1171 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1173 if (!config
->cluster_iam_master
)
1175 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1176 fmtaddr(config
->cluster_master_address
, 0));
1183 cli_print(cli
, "Specify a tunnel to drop");
1187 for (i
= 0; i
< argc
; i
++)
1189 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1191 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1197 cli_print(cli
, "Tunnel %d is not connected", t
);
1203 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1207 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1208 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1214 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1219 if (CLI_HELP_REQUESTED
)
1220 return cli_arg_help(cli
, argc
> 1,
1221 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1223 if (!config
->cluster_iam_master
)
1225 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1226 fmtaddr(config
->cluster_master_address
, 0));
1233 cli_print(cli
, "Specify a session id to drop");
1237 for (i
= 0; i
< argc
; i
++)
1239 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1241 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1245 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1247 cli_print(cli
, "Dropping session %d", s
);
1248 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1252 cli_print(cli
, "Session %d is not active.", s
);
1259 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1265 if (CLI_HELP_REQUESTED
)
1270 return cli_arg_help(cli
, 0,
1271 "USER", "Username of session to snoop", NULL
);
1274 return cli_arg_help(cli
, 0,
1275 "A.B.C.D", "IP address of snoop destination", NULL
);
1278 return cli_arg_help(cli
, 0,
1279 "N", "Port of snoop destination", NULL
);
1283 return cli_arg_help(cli
, 1, NULL
);
1290 if (!config
->cluster_iam_master
)
1292 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1293 fmtaddr(config
->cluster_master_address
, 0));
1300 cli_print(cli
, "Specify username, ip and port");
1304 if (!(s
= sessionbyuser(argv
[0])))
1306 cli_print(cli
, "User %s is not connected", argv
[0]);
1310 ip
= inet_addr(argv
[1]);
1311 if (!ip
|| ip
== INADDR_NONE
)
1313 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1317 port
= atoi(argv
[2]);
1320 cli_print(cli
, "Invalid port %s", argv
[2]);
1324 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1325 cli_session_actions
[s
].snoop_ip
= ip
;
1326 cli_session_actions
[s
].snoop_port
= port
;
1327 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1332 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1337 if (CLI_HELP_REQUESTED
)
1338 return cli_arg_help(cli
, argc
> 1,
1339 "USER", "Username of session to unsnoop", NULL
);
1341 if (!config
->cluster_iam_master
)
1343 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1344 fmtaddr(config
->cluster_master_address
, 0));
1351 cli_print(cli
, "Specify a user to unsnoop");
1355 for (i
= 0; i
< argc
; i
++)
1357 if (!(s
= sessionbyuser(argv
[i
])))
1359 cli_print(cli
, "User %s is not connected", argv
[i
]);
1363 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1364 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1370 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1377 throttle USER - throttle in/out to default rate
1378 throttle USER RATE - throttle in/out to default rate
1379 throttle USER in RATE - throttle input only
1380 throttle USER out RATE - throttle output only
1381 throttle USER in RATE out RATE - throttle both
1384 if (CLI_HELP_REQUESTED
)
1389 return cli_arg_help(cli
, 0,
1390 "USER", "Username of session to throttle", NULL
);
1393 return cli_arg_help(cli
, 1,
1394 "RATE", "Rate in kbps (in and out)",
1395 "in", "Select incoming rate",
1396 "out", "Select outgoing rate", NULL
);
1399 return cli_arg_help(cli
, 1,
1400 "in", "Select incoming rate",
1401 "out", "Select outgoing rate", NULL
);
1404 if (isdigit(argv
[1][0]))
1405 return cli_arg_help(cli
, 1, NULL
);
1408 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1411 return cli_arg_help(cli
, argc
> 1, NULL
);
1415 if (!config
->cluster_iam_master
)
1417 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1418 fmtaddr(config
->cluster_master_address
, 0));
1425 cli_print(cli
, "Specify a user to throttle");
1429 if (!(s
= sessionbyuser(argv
[0])))
1431 cli_print(cli
, "User %s is not connected", argv
[0]);
1437 rate_in
= rate_out
= config
->rl_rate
;
1441 rate_in
= rate_out
= atoi(argv
[1]);
1444 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1448 else if (argc
== 3 || argc
== 5)
1451 for (i
= 1; i
< argc
- 1; i
+= 2)
1454 if (MATCH("in", argv
[i
]))
1455 r
= rate_in
= atoi(argv
[i
+1]);
1456 else if (MATCH("out", argv
[i
]))
1457 r
= rate_out
= atoi(argv
[i
+1]);
1461 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1468 cli_print(cli
, "Invalid arguments");
1472 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1474 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1478 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1479 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1480 cli_session_actions
[s
].throttle_in
= rate_in
;
1482 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1483 cli_session_actions
[s
].throttle_out
= rate_out
;
1485 if (cli_session_actions
[s
].throttle_in
== -1 &&
1486 cli_session_actions
[s
].throttle_out
== -1)
1488 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1492 cli_print(cli
, "Throttling user %s", argv
[0]);
1493 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1498 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1503 if (CLI_HELP_REQUESTED
)
1504 return cli_arg_help(cli
, argc
> 1,
1505 "USER", "Username of session to unthrottle", NULL
);
1507 if (!config
->cluster_iam_master
)
1509 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1510 fmtaddr(config
->cluster_master_address
, 0));
1517 cli_print(cli
, "Specify a user to unthrottle");
1521 for (i
= 0; i
< argc
; i
++)
1523 if (!(s
= sessionbyuser(argv
[i
])))
1525 cli_print(cli
, "User %s is not connected", argv
[i
]);
1529 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1531 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1532 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1536 cli_print(cli
, "User %s not throttled", argv
[i
]);
1543 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1547 if (CLI_HELP_REQUESTED
)
1548 return cli_arg_help(cli
, 1,
1549 "all", "Enable debugging for all except \"data\"",
1550 "critical", "", // FIXME: add descriptions
1560 char *p
= (char *) &debug_flags
;
1561 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1565 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1566 (debug_flags
.critical
) ? " critical" : "",
1567 (debug_flags
.error
) ? " error" : "",
1568 (debug_flags
.warning
) ? " warning" : "",
1569 (debug_flags
.info
) ? " info" : "",
1570 (debug_flags
.calls
) ? " calls" : "",
1571 (debug_flags
.data
) ? " data" : "");
1577 cli_print(cli
, "Debugging off");
1581 for (i
= 0; i
< argc
; i
++)
1583 int len
= strlen(argv
[i
]);
1585 if (argv
[i
][0] == 'c' && len
< 2)
1586 len
= 2; /* distinguish [cr]itical from [ca]lls */
1588 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1589 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1590 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1591 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1592 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1593 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1594 if (!strncmp("all", argv
[i
], len
))
1596 memset(&debug_flags
, 1, sizeof(debug_flags
));
1597 debug_flags
.data
= 0;
1601 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1607 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1611 if (CLI_HELP_REQUESTED
)
1612 return cli_arg_help(cli
, 1,
1613 "all", "Disable all debugging",
1614 "critical", "", // FIXME: add descriptions
1624 memset(&debug_flags
, 0, sizeof(debug_flags
));
1628 for (i
= 0; i
< argc
; i
++)
1630 int len
= strlen(argv
[i
]);
1632 if (argv
[i
][0] == 'c' && len
< 2)
1633 len
= 2; /* distinguish [cr]itical from [ca]lls */
1635 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1636 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1637 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1638 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1639 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1640 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1641 if (!strncmp("all", argv
[i
], len
))
1643 memset(&debug_flags
, 0, sizeof(debug_flags
));
1647 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1653 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1655 int i
, firstfree
= 0;
1657 if (CLI_HELP_REQUESTED
)
1658 return cli_arg_help(cli
, argc
> 1,
1659 "PLUGIN", "Name of plugin to load", NULL
);
1663 cli_print(cli
, "Specify a plugin to load");
1667 for (i
= 0; i
< MAXPLUGINS
; i
++)
1669 if (!*config
->plugins
[i
] && !firstfree
)
1671 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1673 cli_print(cli
, "Plugin is already loaded");
1680 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1681 config
->reload_config
= 1;
1682 cli_print(cli
, "Loading plugin %s", argv
[0]);
1688 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1692 if (CLI_HELP_REQUESTED
)
1693 return cli_arg_help(cli
, argc
> 1,
1694 "PLUGIN", "Name of plugin to unload", NULL
);
1698 cli_print(cli
, "Specify a plugin to remove");
1702 for (i
= 0; i
< MAXPLUGINS
; i
++)
1704 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1706 config
->reload_config
= 1;
1707 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1712 cli_print(cli
, "Plugin is not loaded");
1716 static char *duration(time_t secs
)
1718 static char *buf
= NULL
;
1721 if (!buf
) buf
= calloc(64, 1);
1725 int days
= secs
/ 86400;
1726 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1732 int mins
= secs
/ 60;
1733 int hrs
= mins
/ 60;
1736 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1738 else if (secs
>= 60)
1740 int mins
= secs
/ 60;
1741 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1744 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1749 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1752 char buf
[100], *p
= buf
, *loads
[3];
1753 int i
, num_sessions
= 0;
1755 if (CLI_HELP_REQUESTED
)
1756 return CLI_HELP_NO_ARGS
;
1758 fh
= fopen("/proc/loadavg", "r");
1759 fgets(buf
, 100, fh
);
1762 for (i
= 0; i
< 3; i
++)
1763 loads
[i
] = strdup(strsep(&p
, " "));
1766 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1768 for (i
= 1; i
< MAXSESSION
; i
++)
1769 if (session
[i
].opened
) num_sessions
++;
1771 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1773 duration(time_now
- config
->start_time
),
1775 loads
[0], loads
[1], loads
[2]
1777 for (i
= 0; i
< 3; i
++)
1778 if (loads
[i
]) free(loads
[i
]);
1780 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1785 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1789 if (CLI_HELP_REQUESTED
)
1795 int len
= strlen(argv
[0])-1;
1796 for (i
= 0; config_values
[i
].key
; i
++)
1797 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1798 cli_print(cli
, " %s", config_values
[i
].key
);
1804 return cli_arg_help(cli
, 0,
1805 "VALUE", "Value for variable", NULL
);
1809 return cli_arg_help(cli
, 1, NULL
);
1818 cli_print(cli
, "Specify variable and value");
1822 for (i
= 0; config_values
[i
].key
; i
++)
1824 void *value
= ((void *)config
) + config_values
[i
].offset
;
1825 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1827 // Found a value to set
1828 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1829 switch (config_values
[i
].type
)
1832 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1835 *(int *)value
= atoi(argv
[1]);
1838 *(unsigned long *)value
= atol(argv
[1]);
1841 *(short *)value
= atoi(argv
[1]);
1844 *(unsigned *)value
= inet_addr(argv
[1]);
1847 parsemac(argv
[1], (char *)value
);
1850 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1856 cli_print(cli
, "Unknown variable type");
1859 config
->reload_config
= 1;
1864 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1868 int regular_stuff(struct cli_def
*cli
)
1874 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1876 char *m
= ringbuffer
->buffer
[i
].message
;
1882 switch (ringbuffer
->buffer
[i
].level
)
1884 case 0: show
= debug_flags
.critical
; break;
1885 case 1: show
= debug_flags
.error
; break;
1886 case 2: show
= debug_flags
.warning
; break;
1887 case 3: show
= debug_flags
.info
; break;
1888 case 4: show
= debug_flags
.calls
; break;
1889 case 5: show
= debug_flags
.data
; break;
1892 if (!show
) continue;
1894 if (!(p
= strchr(m
, '\n')))
1897 cli_print(cli
, "\r%s-%u-%u %.*s",
1898 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1899 ringbuffer
->buffer
[i
].tunnel
,
1900 ringbuffer
->buffer
[i
].session
,
1906 debug_rb_tail
= ringbuffer
->tail
;
1914 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1918 if (CLI_HELP_REQUESTED
)
1919 return cli_arg_help(cli
, argc
> 1,
1920 "<1-65535>", "Autonomous system number", NULL
);
1922 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1924 cli_print(cli
, "Invalid autonomous system number");
1928 if (bgp_configured
&& as
!= config
->as_number
)
1930 cli_print(cli
, "Can't change local AS on a running system");
1934 config
->as_number
= as
;
1935 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1940 static int find_bgp_neighbour(char const *name
)
1945 in_addr_t addrs
[4] = { 0 };
1948 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1951 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1952 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1954 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1956 if (!config
->neighbour
[i
].name
[0])
1958 if (new == -1) new = i
;
1962 if (!strcmp(name
, config
->neighbour
[i
].name
))
1965 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1968 for (a
= h
->h_addr_list
; *a
; a
++)
1971 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1972 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1980 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1986 if (CLI_HELP_REQUESTED
)
1991 return cli_arg_help(cli
, 0,
1992 "A.B.C.D", "BGP neighbour address",
1993 "NAME", "BGP neighbour name",
1997 return cli_arg_help(cli
, 0,
1998 "remote-as", "Set remote autonomous system number",
1999 "timers", "Set timers",
2003 if (MATCH("remote-as", argv
[1]))
2004 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2006 if (MATCH("timers", argv
[1]))
2009 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2012 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2014 if (argc
== 5 && !argv
[4][1])
2015 return cli_arg_help(cli
, 1, NULL
);
2024 cli_print(cli
, "Invalid arguments");
2028 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2030 cli_print(cli
, "Invalid neighbour");
2036 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2040 if (MATCH("remote-as", argv
[1]))
2042 int as
= atoi(argv
[2]);
2043 if (as
< 0 || as
> 65535)
2045 cli_print(cli
, "Invalid autonomous system number");
2049 if (!config
->neighbour
[i
].name
[0])
2051 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2052 config
->neighbour
[i
].keepalive
= -1;
2053 config
->neighbour
[i
].hold
= -1;
2056 config
->neighbour
[i
].as
= as
;
2060 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2062 cli_print(cli
, "Invalid arguments");
2066 if (!config
->neighbour
[i
].name
[0])
2068 cli_print(cli
, "Specify remote-as first");
2072 keepalive
= atoi(argv
[2]);
2073 hold
= atoi(argv
[3]);
2075 if (keepalive
< 1 || keepalive
> 65535)
2077 cli_print(cli
, "Invalid keepalive time");
2081 if (hold
< 3 || hold
> 65535)
2083 cli_print(cli
, "Invalid hold time");
2087 if (keepalive
== BGP_KEEPALIVE_TIME
)
2088 keepalive
= -1; // using default value
2090 if (hold
== BGP_HOLD_TIME
)
2093 config
->neighbour
[i
].keepalive
= keepalive
;
2094 config
->neighbour
[i
].hold
= hold
;
2099 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2103 if (CLI_HELP_REQUESTED
)
2104 return cli_arg_help(cli
, argc
> 0,
2105 "A.B.C.D", "BGP neighbour address",
2106 "NAME", "BGP neighbour name",
2111 cli_print(cli
, "Specify a BGP neighbour");
2115 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2117 cli_print(cli
, "Invalid neighbour");
2121 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2123 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2127 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2131 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2137 if (!bgp_configured
)
2140 if (CLI_HELP_REQUESTED
)
2141 return cli_arg_help(cli
, 1,
2142 "A.B.C.D", "BGP neighbour address",
2143 "NAME", "BGP neighbour name",
2146 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2147 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2151 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2153 if (!*bgp_peers
[i
].name
)
2156 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2157 if (argc
&& strcmp(addr
, argv
[0]) &&
2158 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2164 cli_print(cli
, "Peer AS Address "
2165 "State Retries Retry in Route Pend Timers");
2166 cli_print(cli
, "------------------ ----- --------------- "
2167 "----------- ------- -------- ----- ---- ---------");
2170 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2174 bgp_state_str(bgp_peers
[i
].state
),
2175 bgp_peers
[i
].retry_count
,
2176 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2177 bgp_peers
[i
].routing
? "yes" : "no",
2178 bgp_peers
[i
].update_routes
? "yes" : "no",
2179 bgp_peers
[i
].keepalive
,
2186 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2191 if (!bgp_configured
)
2194 if (CLI_HELP_REQUESTED
)
2195 return cli_arg_help(cli
, 1,
2196 "A.B.C.D", "BGP neighbour address",
2197 "NAME", "BGP neighbour name",
2200 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2202 if (bgp_peers
[i
].state
!= Established
)
2205 if (!bgp_peers
[i
].routing
)
2208 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2209 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2212 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2213 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2219 static int cmd_no_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]) &&
2243 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2246 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2247 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2253 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2258 if (!bgp_configured
)
2261 if (CLI_HELP_REQUESTED
)
2262 return cli_arg_help(cli
, 1,
2263 "A.B.C.D", "BGP neighbour address",
2264 "NAME", "BGP neighbour name",
2267 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2269 if (!*bgp_peers
[i
].name
)
2272 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2273 if (argc
&& strcmp(addr
, argv
[0]) &&
2274 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2277 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2278 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2286 static int find_access_list(char const *name
)
2290 for (i
= 0; i
< MAXFILTER
; i
++)
2291 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2297 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2301 if (CLI_HELP_REQUESTED
)
2306 return cli_arg_help(cli
, 0,
2307 "standard", "Standard syntax",
2308 "extended", "Extended syntax",
2312 return cli_arg_help(cli
, argv
[1][1],
2313 "NAME", "Access-list name",
2317 if (argc
== 3 && !argv
[2][1])
2318 return cli_arg_help(cli
, 1, NULL
);
2326 cli_print(cli
, "Specify access-list type and name");
2330 if (MATCH("standard", argv
[0]))
2332 else if (MATCH("extended", argv
[0]))
2336 cli_print(cli
, "Invalid access-list type");
2340 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2341 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2343 cli_print(cli
, "Invalid access-list name");
2347 filt
= find_access_list(argv
[1]);
2352 cli_print(cli
, "Too many access-lists");
2357 if (!*ip_filters
[filt
].name
)
2359 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2360 strcpy(ip_filters
[filt
].name
, argv
[1]);
2361 ip_filters
[filt
].extended
= extended
;
2363 else if (ip_filters
[filt
].extended
!= extended
)
2365 cli_print(cli
, "Access-list is %s",
2366 ip_filters
[filt
].extended
? "extended" : "standard");
2371 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2375 if (filt
< 0 || !*ip_filters
[filt
].name
)
2377 cli_print(cli
, "Access-list not defined");
2382 if (ip_filters
[filt
].used
)
2384 cli_print(cli
, "Access-list in use");
2388 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2392 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2394 return access_list(cli
, argv
, argc
, 1);
2397 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2399 return access_list(cli
, argv
, argc
, 0);
2402 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2404 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2405 return sprintf(buf
, " any");
2407 if (wild
== INADDR_ANY
)
2408 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2410 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2413 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2417 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2418 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2419 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2420 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2421 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2427 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2429 static char buf
[256];
2432 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2435 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2436 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2439 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2443 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2444 p
+= show_ports(p
, &rule
->src_ports
);
2446 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2447 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2448 p
+= show_ports(p
, &rule
->dst_ports
);
2450 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2452 switch (rule
->tcp_flag_op
)
2454 case FILTER_FLAG_OP_EST
:
2455 p
+= sprintf(p
, " established");
2458 case FILTER_FLAG_OP_ANY
:
2459 case FILTER_FLAG_OP_ALL
:
2460 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2461 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2462 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2463 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2464 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2465 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2466 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2467 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2468 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2469 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2470 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2471 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2472 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2478 p
+= sprintf(p
, " fragments");
2483 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2485 static ip_filter_rulet rule
;
2486 struct in_addr addr
;
2490 if (CLI_HELP_REQUESTED
)
2494 cli_arg_help(cli
, 0,
2495 "ip", "Match IP packets",
2496 "tcp", "Match TCP packets",
2497 "udp", "Match UDP packets",
2503 // *sigh*, too darned complex
2504 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2510 cli_print(cli
, "Specify rule details");
2514 memset(&rule
, 0, sizeof(rule
));
2515 rule
.action
= (command
[0] == 'p')
2516 ? FILTER_ACTION_PERMIT
2517 : FILTER_ACTION_DENY
;
2519 if (MATCH("ip", argv
[0]))
2520 rule
.proto
= IPPROTO_IP
;
2521 else if (MATCH("udp", argv
[0]))
2522 rule
.proto
= IPPROTO_UDP
;
2523 else if (MATCH("tcp", argv
[0]))
2524 rule
.proto
= IPPROTO_TCP
;
2527 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2531 for (a
= 1, i
= 0; i
< 2; i
++)
2535 ip_filter_portt
*port
;
2540 wild
= &rule
.src_wild
;
2541 port
= &rule
.src_ports
;
2546 wild
= &rule
.dst_wild
;
2547 port
= &rule
.dst_ports
;
2550 cli_print(cli
, "Specify destination");
2555 if (MATCH("any", argv
[a
]))
2558 *wild
= INADDR_BROADCAST
;
2561 else if (MATCH("host", argv
[a
]))
2565 cli_print(cli
, "Specify host ip address");
2569 if (!inet_aton(argv
[a
], &addr
))
2571 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2583 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2587 if (!inet_aton(argv
[a
], &addr
))
2589 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2595 if (!inet_aton(argv
[++a
], &addr
))
2597 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2601 *wild
= addr
.s_addr
;
2605 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2609 if (MATCH("eq", argv
[a
]))
2610 port
->op
= FILTER_PORT_OP_EQ
;
2611 else if (MATCH("neq", argv
[a
]))
2612 port
->op
= FILTER_PORT_OP_NEQ
;
2613 else if (MATCH("gt", argv
[a
]))
2614 port
->op
= FILTER_PORT_OP_GT
;
2615 else if (MATCH("lt", argv
[a
]))
2616 port
->op
= FILTER_PORT_OP_LT
;
2617 else if (MATCH("range", argv
[a
]))
2618 port
->op
= FILTER_PORT_OP_RANGE
;
2625 cli_print(cli
, "Specify port");
2629 if (!(port
->port
= atoi(argv
[a
])))
2631 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2636 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2641 cli_print(cli
, "Specify port");
2645 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2647 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2654 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2656 if (MATCH("established", argv
[a
]))
2658 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2661 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2662 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2664 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2665 ? FILTER_FLAG_OP_ANY
2666 : FILTER_FLAG_OP_ALL
;
2670 cli_print(cli
, "Specify tcp flags");
2674 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2678 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2680 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2681 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2682 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2683 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2684 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2685 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2688 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2697 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2699 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2701 cli_print(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2711 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2718 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2720 static ip_filter_rulet rule
;
2721 struct in_addr addr
;
2723 if (CLI_HELP_REQUESTED
)
2727 cli_arg_help(cli
, argv
[0][1],
2728 "A.B.C.D", "Source address",
2729 "any", "Any source address",
2730 "host", "Source host",
2736 if (MATCH("any", argv
[0]))
2738 if (argc
== 2 && !argv
[1][1])
2739 cli_arg_help(cli
, 1, NULL
);
2741 else if (MATCH("host", argv
[0]))
2745 cli_arg_help(cli
, argv
[1][1],
2746 "A.B.C.D", "Host address",
2749 else if (argc
== 3 && !argv
[2][1])
2750 cli_arg_help(cli
, 1, NULL
);
2756 cli_arg_help(cli
, 1,
2757 "A.B.C.D", "Wildcard bits",
2760 else if (argc
== 3 && !argv
[2][1])
2761 cli_arg_help(cli
, 1, NULL
);
2769 cli_print(cli
, "Specify rule details");
2773 memset(&rule
, 0, sizeof(rule
));
2774 rule
.action
= (command
[0] == 'p')
2775 ? FILTER_ACTION_PERMIT
2776 : FILTER_ACTION_DENY
;
2778 rule
.proto
= IPPROTO_IP
;
2779 if (MATCH("any", argv
[0]))
2781 rule
.src_ip
= INADDR_ANY
;
2782 rule
.src_wild
= INADDR_BROADCAST
;
2784 else if (MATCH("host", argv
[0]))
2788 cli_print(cli
, "Specify host ip address");
2792 if (!inet_aton(argv
[1], &addr
))
2794 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2798 rule
.src_ip
= addr
.s_addr
;
2799 rule
.src_wild
= INADDR_ANY
;
2805 cli_print(cli
, "Specify source ip address and wildcard");
2809 if (!inet_aton(argv
[0], &addr
))
2811 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2815 rule
.src_ip
= addr
.s_addr
;
2819 if (!inet_aton(argv
[1], &addr
))
2821 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2825 rule
.src_wild
= addr
.s_addr
;
2828 rule
.src_wild
= INADDR_ANY
;
2834 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2837 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2838 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2839 : access_list_rule_std(cli
, command
, argv
, argc
);
2844 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2846 if (!ip_filters
[filt
].rules
[i
].action
)
2848 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2852 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2856 cli_print(cli
, "Too many rules");
2860 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2865 /* filter USER {in|out} FILTER ... */
2866 if (CLI_HELP_REQUESTED
)
2871 return cli_arg_help(cli
, 0,
2872 "USER", "Username of session to filter", NULL
);
2876 return cli_arg_help(cli
, 0,
2877 "in", "Set incoming filter",
2878 "out", "Set outgoing filter", NULL
);
2882 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2883 "NAME", "Filter name", NULL
);
2886 return cli_arg_help(cli
, argc
> 1, NULL
);
2890 if (!config
->cluster_iam_master
)
2892 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2893 fmtaddr(config
->cluster_master_address
, 0));
2898 if (argc
!= 3 && argc
!= 5)
2900 cli_print(cli
, "Specify a user and filters");
2904 if (!(s
= sessionbyuser(argv
[0])))
2906 cli_print(cli
, "User %s is not connected", argv
[0]);
2910 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2911 for (i
= 1; i
< argc
; i
+= 2)
2916 if (MATCH("in", argv
[i
]))
2918 if (session
[s
].filter_in
)
2920 cli_print(cli
, "Input already filtered");
2923 f
= &cli_session_actions
[s
].filter_in
;
2925 else if (MATCH("out", argv
[i
]))
2927 if (session
[s
].filter_out
)
2929 cli_print(cli
, "Output already filtered");
2932 f
= &cli_session_actions
[s
].filter_out
;
2936 cli_print(cli
, "Invalid filter specification");
2940 v
= find_access_list(argv
[i
+1]);
2941 if (v
< 0 || !*ip_filters
[v
].name
)
2943 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2950 cli_print(cli
, "Filtering user %s", argv
[0]);
2951 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2956 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2961 if (CLI_HELP_REQUESTED
)
2962 return cli_arg_help(cli
, argc
> 1,
2963 "USER", "Username of session to remove filters from", NULL
);
2965 if (!config
->cluster_iam_master
)
2967 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2968 fmtaddr(config
->cluster_master_address
, 0));
2975 cli_print(cli
, "Specify a user to remove filters from");
2979 for (i
= 0; i
< argc
; i
++)
2981 if (!(s
= sessionbyuser(argv
[i
])))
2983 cli_print(cli
, "User %s is not connected", argv
[i
]);
2987 if (session
[s
].filter_in
|| session
[s
].filter_out
)
2989 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
2990 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
2994 cli_print(cli
, "User %s not filtered", argv
[i
]);
3001 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3005 if (CLI_HELP_REQUESTED
)
3006 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3010 cli_print(cli
, "Specify a filter name");
3014 for (i
= 0; i
< argc
; i
++)
3016 int f
= find_access_list(argv
[i
]);
3017 ip_filter_rulet
*rules
;
3019 if (f
< 0 || !*ip_filters
[f
].name
)
3021 cli_print(cli
, "Access-list %s not defined", argv
[i
]);
3028 cli_print(cli
, "%s IP access list %s",
3029 ip_filters
[f
].extended
? "Extended" : "Standard",
3030 ip_filters
[f
].name
);
3032 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3034 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3036 cli_print(cli
, "%s (%d match%s)", r
,
3037 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3039 cli_print(cli
, "%s", r
);
3046 // Convert a string in the form of abcd.ef12.3456 into char[6]
3047 void parsemac(char *string
, char mac
[6])
3049 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)
3051 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)