1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.43.2.1 2005/01/10 07:08:12 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 %8s %8s %8s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
649 cli_print(cli
, "%-10s %10u %8u %8u %8u", "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 %8u %8u", "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 %8s %8s %8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
661 cli_print(cli
, "%-10s %10u %8u %8u", "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 %8u %8u %8u", "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 cli_print(cli
, "Counters cleared");
1117 SET_STAT(last_reset
, time(NULL
));
1121 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1126 if (CLI_HELP_REQUESTED
)
1127 return cli_arg_help(cli
, argc
> 1,
1128 "USER", "Username of session to drop", NULL
);
1130 if (!config
->cluster_iam_master
)
1132 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1133 fmtaddr(config
->cluster_master_address
, 0));
1140 cli_print(cli
, "Specify a user to drop");
1144 for (i
= 0; i
< argc
; i
++)
1146 if (!(s
= sessionbyuser(argv
[i
])))
1148 cli_print(cli
, "User %s is not connected", argv
[i
]);
1152 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1154 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1155 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1162 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1167 if (CLI_HELP_REQUESTED
)
1168 return cli_arg_help(cli
, argc
> 1,
1169 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1171 if (!config
->cluster_iam_master
)
1173 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1174 fmtaddr(config
->cluster_master_address
, 0));
1181 cli_print(cli
, "Specify a tunnel to drop");
1185 for (i
= 0; i
< argc
; i
++)
1187 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1189 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1195 cli_print(cli
, "Tunnel %d is not connected", t
);
1201 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1205 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1206 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1212 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1217 if (CLI_HELP_REQUESTED
)
1218 return cli_arg_help(cli
, argc
> 1,
1219 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1221 if (!config
->cluster_iam_master
)
1223 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1224 fmtaddr(config
->cluster_master_address
, 0));
1231 cli_print(cli
, "Specify a session id to drop");
1235 for (i
= 0; i
< argc
; i
++)
1237 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1239 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1243 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1245 cli_print(cli
, "Dropping session %d", s
);
1246 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1250 cli_print(cli
, "Session %d is not active.", s
);
1257 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1263 if (CLI_HELP_REQUESTED
)
1268 return cli_arg_help(cli
, 0,
1269 "USER", "Username of session to snoop", NULL
);
1272 return cli_arg_help(cli
, 0,
1273 "A.B.C.D", "IP address of snoop destination", NULL
);
1276 return cli_arg_help(cli
, 0,
1277 "N", "Port of snoop destination", NULL
);
1281 return cli_arg_help(cli
, 1, NULL
);
1288 if (!config
->cluster_iam_master
)
1290 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1291 fmtaddr(config
->cluster_master_address
, 0));
1298 cli_print(cli
, "Specify username, ip and port");
1302 if (!(s
= sessionbyuser(argv
[0])))
1304 cli_print(cli
, "User %s is not connected", argv
[0]);
1308 ip
= inet_addr(argv
[1]);
1309 if (!ip
|| ip
== INADDR_NONE
)
1311 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1315 port
= atoi(argv
[2]);
1318 cli_print(cli
, "Invalid port %s", argv
[2]);
1322 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1323 cli_session_actions
[s
].snoop_ip
= ip
;
1324 cli_session_actions
[s
].snoop_port
= port
;
1325 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1330 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1335 if (CLI_HELP_REQUESTED
)
1336 return cli_arg_help(cli
, argc
> 1,
1337 "USER", "Username of session to unsnoop", NULL
);
1339 if (!config
->cluster_iam_master
)
1341 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1342 fmtaddr(config
->cluster_master_address
, 0));
1349 cli_print(cli
, "Specify a user to unsnoop");
1353 for (i
= 0; i
< argc
; i
++)
1355 if (!(s
= sessionbyuser(argv
[i
])))
1357 cli_print(cli
, "User %s is not connected", argv
[i
]);
1361 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1362 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1368 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1375 throttle USER - throttle in/out to default rate
1376 throttle USER RATE - throttle in/out to default rate
1377 throttle USER in RATE - throttle input only
1378 throttle USER out RATE - throttle output only
1379 throttle USER in RATE out RATE - throttle both
1382 if (CLI_HELP_REQUESTED
)
1387 return cli_arg_help(cli
, 0,
1388 "USER", "Username of session to throttle", NULL
);
1391 return cli_arg_help(cli
, 1,
1392 "RATE", "Rate in kbps (in and out)",
1393 "in", "Select incoming rate",
1394 "out", "Select outgoing rate", NULL
);
1397 return cli_arg_help(cli
, 1,
1398 "in", "Select incoming rate",
1399 "out", "Select outgoing rate", NULL
);
1402 if (isdigit(argv
[1][0]))
1403 return cli_arg_help(cli
, 1, NULL
);
1406 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1409 return cli_arg_help(cli
, argc
> 1, NULL
);
1413 if (!config
->cluster_iam_master
)
1415 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1416 fmtaddr(config
->cluster_master_address
, 0));
1423 cli_print(cli
, "Specify a user to throttle");
1427 if (!(s
= sessionbyuser(argv
[0])))
1429 cli_print(cli
, "User %s is not connected", argv
[0]);
1435 rate_in
= rate_out
= config
->rl_rate
;
1439 rate_in
= rate_out
= atoi(argv
[1]);
1442 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1446 else if (argc
== 3 || argc
== 5)
1449 for (i
= 1; i
< argc
- 1; i
+= 2)
1452 if (MATCH("in", argv
[i
]))
1453 r
= rate_in
= atoi(argv
[i
+1]);
1454 else if (MATCH("out", argv
[i
]))
1455 r
= rate_out
= atoi(argv
[i
+1]);
1459 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1466 cli_print(cli
, "Invalid arguments");
1470 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1472 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1476 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1477 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1478 cli_session_actions
[s
].throttle_in
= rate_in
;
1480 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1481 cli_session_actions
[s
].throttle_out
= rate_out
;
1483 if (cli_session_actions
[s
].throttle_in
== -1 &&
1484 cli_session_actions
[s
].throttle_out
== -1)
1486 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1490 cli_print(cli
, "Throttling user %s", argv
[0]);
1491 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1496 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1501 if (CLI_HELP_REQUESTED
)
1502 return cli_arg_help(cli
, argc
> 1,
1503 "USER", "Username of session to unthrottle", NULL
);
1505 if (!config
->cluster_iam_master
)
1507 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1508 fmtaddr(config
->cluster_master_address
, 0));
1515 cli_print(cli
, "Specify a user to unthrottle");
1519 for (i
= 0; i
< argc
; i
++)
1521 if (!(s
= sessionbyuser(argv
[i
])))
1523 cli_print(cli
, "User %s is not connected", argv
[i
]);
1527 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1529 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1530 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1534 cli_print(cli
, "User %s not throttled", argv
[i
]);
1541 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1545 if (CLI_HELP_REQUESTED
)
1546 return cli_arg_help(cli
, 1,
1547 "all", "Enable debugging for all except \"data\"",
1548 "critical", "", // FIXME: add descriptions
1558 char *p
= (char *) &debug_flags
;
1559 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1563 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1564 (debug_flags
.critical
) ? " critical" : "",
1565 (debug_flags
.error
) ? " error" : "",
1566 (debug_flags
.warning
) ? " warning" : "",
1567 (debug_flags
.info
) ? " info" : "",
1568 (debug_flags
.calls
) ? " calls" : "",
1569 (debug_flags
.data
) ? " data" : "");
1575 cli_print(cli
, "Debugging off");
1579 for (i
= 0; i
< argc
; i
++)
1581 int len
= strlen(argv
[i
]);
1583 if (argv
[i
][0] == 'c' && len
< 2)
1584 len
= 2; /* distinguish [cr]itical from [ca]lls */
1586 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1587 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1588 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1589 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1590 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1591 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1592 if (!strncmp("all", argv
[i
], len
))
1594 memset(&debug_flags
, 1, sizeof(debug_flags
));
1595 debug_flags
.data
= 0;
1599 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1605 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1609 if (CLI_HELP_REQUESTED
)
1610 return cli_arg_help(cli
, 1,
1611 "all", "Disable all debugging",
1612 "critical", "", // FIXME: add descriptions
1622 memset(&debug_flags
, 0, sizeof(debug_flags
));
1626 for (i
= 0; i
< argc
; i
++)
1628 int len
= strlen(argv
[i
]);
1630 if (argv
[i
][0] == 'c' && len
< 2)
1631 len
= 2; /* distinguish [cr]itical from [ca]lls */
1633 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1634 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1635 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1636 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1637 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1638 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1639 if (!strncmp("all", argv
[i
], len
))
1641 memset(&debug_flags
, 0, sizeof(debug_flags
));
1645 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1651 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1653 int i
, firstfree
= 0;
1655 if (CLI_HELP_REQUESTED
)
1656 return cli_arg_help(cli
, argc
> 1,
1657 "PLUGIN", "Name of plugin to load", NULL
);
1661 cli_print(cli
, "Specify a plugin to load");
1665 for (i
= 0; i
< MAXPLUGINS
; i
++)
1667 if (!*config
->plugins
[i
] && !firstfree
)
1669 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1671 cli_print(cli
, "Plugin is already loaded");
1678 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1679 config
->reload_config
= 1;
1680 cli_print(cli
, "Loading plugin %s", argv
[0]);
1686 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1690 if (CLI_HELP_REQUESTED
)
1691 return cli_arg_help(cli
, argc
> 1,
1692 "PLUGIN", "Name of plugin to unload", NULL
);
1696 cli_print(cli
, "Specify a plugin to remove");
1700 for (i
= 0; i
< MAXPLUGINS
; i
++)
1702 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1704 config
->reload_config
= 1;
1705 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1710 cli_print(cli
, "Plugin is not loaded");
1714 static char *duration(time_t secs
)
1716 static char *buf
= NULL
;
1719 if (!buf
) buf
= calloc(64, 1);
1723 int days
= secs
/ 86400;
1724 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1730 int mins
= secs
/ 60;
1731 int hrs
= mins
/ 60;
1734 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1736 else if (secs
>= 60)
1738 int mins
= secs
/ 60;
1739 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1742 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1747 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1750 char buf
[100], *p
= buf
, *loads
[3];
1751 int i
, num_sessions
= 0;
1753 if (CLI_HELP_REQUESTED
)
1754 return CLI_HELP_NO_ARGS
;
1756 fh
= fopen("/proc/loadavg", "r");
1757 fgets(buf
, 100, fh
);
1760 for (i
= 0; i
< 3; i
++)
1761 loads
[i
] = strdup(strsep(&p
, " "));
1764 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1766 for (i
= 1; i
< MAXSESSION
; i
++)
1767 if (session
[i
].opened
) num_sessions
++;
1769 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1771 duration(time_now
- config
->start_time
),
1773 loads
[0], loads
[1], loads
[2]
1775 for (i
= 0; i
< 3; i
++)
1776 if (loads
[i
]) free(loads
[i
]);
1778 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1783 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1787 if (CLI_HELP_REQUESTED
)
1793 int len
= strlen(argv
[0])-1;
1794 for (i
= 0; config_values
[i
].key
; i
++)
1795 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1796 cli_print(cli
, " %s", config_values
[i
].key
);
1802 return cli_arg_help(cli
, 0,
1803 "VALUE", "Value for variable", NULL
);
1807 return cli_arg_help(cli
, 1, NULL
);
1816 cli_print(cli
, "Specify variable and value");
1820 for (i
= 0; config_values
[i
].key
; i
++)
1822 void *value
= ((void *)config
) + config_values
[i
].offset
;
1823 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1825 // Found a value to set
1826 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1827 switch (config_values
[i
].type
)
1830 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1833 *(int *)value
= atoi(argv
[1]);
1836 *(unsigned long *)value
= atol(argv
[1]);
1839 *(short *)value
= atoi(argv
[1]);
1842 *(unsigned *)value
= inet_addr(argv
[1]);
1845 parsemac(argv
[1], (char *)value
);
1848 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1854 cli_print(cli
, "Unknown variable type");
1857 config
->reload_config
= 1;
1862 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1866 int regular_stuff(struct cli_def
*cli
)
1872 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1874 char *m
= ringbuffer
->buffer
[i
].message
;
1880 switch (ringbuffer
->buffer
[i
].level
)
1882 case 0: show
= debug_flags
.critical
; break;
1883 case 1: show
= debug_flags
.error
; break;
1884 case 2: show
= debug_flags
.warning
; break;
1885 case 3: show
= debug_flags
.info
; break;
1886 case 4: show
= debug_flags
.calls
; break;
1887 case 5: show
= debug_flags
.data
; break;
1890 if (!show
) continue;
1892 if (!(p
= strchr(m
, '\n')))
1895 cli_print(cli
, "\r%s-%u-%u %.*s",
1896 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1897 ringbuffer
->buffer
[i
].tunnel
,
1898 ringbuffer
->buffer
[i
].session
,
1904 debug_rb_tail
= ringbuffer
->tail
;
1912 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1916 if (CLI_HELP_REQUESTED
)
1917 return cli_arg_help(cli
, argc
> 1,
1918 "<1-65535>", "Autonomous system number", NULL
);
1920 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1922 cli_print(cli
, "Invalid autonomous system number");
1926 if (bgp_configured
&& as
!= config
->as_number
)
1928 cli_print(cli
, "Can't change local AS on a running system");
1932 config
->as_number
= as
;
1933 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1938 static int find_bgp_neighbour(char const *name
)
1943 in_addr_t addrs
[4] = { 0 };
1946 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1949 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1950 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1952 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1954 if (!config
->neighbour
[i
].name
[0])
1956 if (new == -1) new = i
;
1960 if (!strcmp(name
, config
->neighbour
[i
].name
))
1963 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1966 for (a
= h
->h_addr_list
; *a
; a
++)
1969 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1970 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1978 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1984 if (CLI_HELP_REQUESTED
)
1989 return cli_arg_help(cli
, 0,
1990 "A.B.C.D", "BGP neighbour address",
1991 "NAME", "BGP neighbour name",
1995 return cli_arg_help(cli
, 0,
1996 "remote-as", "Set remote autonomous system number",
1997 "timers", "Set timers",
2001 if (MATCH("remote-as", argv
[1]))
2002 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2004 if (MATCH("timers", argv
[1]))
2007 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2010 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2012 if (argc
== 5 && !argv
[4][1])
2013 return cli_arg_help(cli
, 1, NULL
);
2022 cli_print(cli
, "Invalid arguments");
2026 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2028 cli_print(cli
, "Invalid neighbour");
2034 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2038 if (MATCH("remote-as", argv
[1]))
2040 int as
= atoi(argv
[2]);
2041 if (as
< 0 || as
> 65535)
2043 cli_print(cli
, "Invalid autonomous system number");
2047 if (!config
->neighbour
[i
].name
[0])
2049 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2050 config
->neighbour
[i
].keepalive
= -1;
2051 config
->neighbour
[i
].hold
= -1;
2054 config
->neighbour
[i
].as
= as
;
2058 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2060 cli_print(cli
, "Invalid arguments");
2064 if (!config
->neighbour
[i
].name
[0])
2066 cli_print(cli
, "Specify remote-as first");
2070 keepalive
= atoi(argv
[2]);
2071 hold
= atoi(argv
[3]);
2073 if (keepalive
< 1 || keepalive
> 65535)
2075 cli_print(cli
, "Invalid keepalive time");
2079 if (hold
< 3 || hold
> 65535)
2081 cli_print(cli
, "Invalid hold time");
2085 if (keepalive
== BGP_KEEPALIVE_TIME
)
2086 keepalive
= -1; // using default value
2088 if (hold
== BGP_HOLD_TIME
)
2091 config
->neighbour
[i
].keepalive
= keepalive
;
2092 config
->neighbour
[i
].hold
= hold
;
2097 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2101 if (CLI_HELP_REQUESTED
)
2102 return cli_arg_help(cli
, argc
> 0,
2103 "A.B.C.D", "BGP neighbour address",
2104 "NAME", "BGP neighbour name",
2109 cli_print(cli
, "Specify a BGP neighbour");
2113 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2115 cli_print(cli
, "Invalid neighbour");
2119 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2121 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2125 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2129 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2135 if (!bgp_configured
)
2138 if (CLI_HELP_REQUESTED
)
2139 return cli_arg_help(cli
, 1,
2140 "A.B.C.D", "BGP neighbour address",
2141 "NAME", "BGP neighbour name",
2144 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2145 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2149 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2151 if (!*bgp_peers
[i
].name
)
2154 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2155 if (argc
&& strcmp(addr
, argv
[0]) &&
2156 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2162 cli_print(cli
, "Peer AS Address "
2163 "State Retries Retry in Route Pend Timers");
2164 cli_print(cli
, "------------------ ----- --------------- "
2165 "----------- ------- -------- ----- ---- ---------");
2168 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2172 bgp_state_str(bgp_peers
[i
].state
),
2173 bgp_peers
[i
].retry_count
,
2174 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2175 bgp_peers
[i
].routing
? "yes" : "no",
2176 bgp_peers
[i
].update_routes
? "yes" : "no",
2177 bgp_peers
[i
].keepalive
,
2184 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2189 if (!bgp_configured
)
2192 if (CLI_HELP_REQUESTED
)
2193 return cli_arg_help(cli
, 1,
2194 "A.B.C.D", "BGP neighbour address",
2195 "NAME", "BGP neighbour name",
2198 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2200 if (bgp_peers
[i
].state
!= Established
)
2203 if (!bgp_peers
[i
].routing
)
2206 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2207 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2210 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2211 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2217 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2222 if (!bgp_configured
)
2225 if (CLI_HELP_REQUESTED
)
2226 return cli_arg_help(cli
, 1,
2227 "A.B.C.D", "BGP neighbour address",
2228 "NAME", "BGP neighbour name",
2231 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2233 if (bgp_peers
[i
].state
!= Established
)
2236 if (bgp_peers
[i
].routing
)
2239 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2240 if (argc
&& strcmp(addr
, argv
[0]) &&
2241 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2244 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2245 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2251 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2256 if (!bgp_configured
)
2259 if (CLI_HELP_REQUESTED
)
2260 return cli_arg_help(cli
, 1,
2261 "A.B.C.D", "BGP neighbour address",
2262 "NAME", "BGP neighbour name",
2265 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2267 if (!*bgp_peers
[i
].name
)
2270 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2271 if (argc
&& strcmp(addr
, argv
[0]) &&
2272 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2275 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2276 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2284 static int find_access_list(char const *name
)
2288 for (i
= 0; i
< MAXFILTER
; i
++)
2289 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2295 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2299 if (CLI_HELP_REQUESTED
)
2304 return cli_arg_help(cli
, 0,
2305 "standard", "Standard syntax",
2306 "extended", "Extended syntax",
2310 return cli_arg_help(cli
, argv
[1][1],
2311 "NAME", "Access-list name",
2315 if (argc
== 3 && !argv
[2][1])
2316 return cli_arg_help(cli
, 1, NULL
);
2324 cli_print(cli
, "Specify access-list type and name");
2328 if (MATCH("standard", argv
[0]))
2330 else if (MATCH("extended", argv
[0]))
2334 cli_print(cli
, "Invalid access-list type");
2338 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2339 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2341 cli_print(cli
, "Invalid access-list name");
2345 filt
= find_access_list(argv
[1]);
2350 cli_print(cli
, "Too many access-lists");
2355 if (!*ip_filters
[filt
].name
)
2357 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2358 strcpy(ip_filters
[filt
].name
, argv
[1]);
2359 ip_filters
[filt
].extended
= extended
;
2361 else if (ip_filters
[filt
].extended
!= extended
)
2363 cli_print(cli
, "Access-list is %s",
2364 ip_filters
[filt
].extended
? "extended" : "standard");
2369 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2373 if (filt
< 0 || !*ip_filters
[filt
].name
)
2375 cli_print(cli
, "Access-list not defined");
2380 if (ip_filters
[filt
].used
)
2382 cli_print(cli
, "Access-list in use");
2386 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2390 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2392 return access_list(cli
, argv
, argc
, 1);
2395 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2397 return access_list(cli
, argv
, argc
, 0);
2400 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2402 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2403 return sprintf(buf
, " any");
2405 if (wild
== INADDR_ANY
)
2406 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2408 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2411 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2415 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2416 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2417 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2418 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2419 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2425 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2427 static char buf
[256];
2430 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2433 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2434 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2437 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2441 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2442 p
+= show_ports(p
, &rule
->src_ports
);
2444 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2445 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2446 p
+= show_ports(p
, &rule
->dst_ports
);
2448 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2450 switch (rule
->tcp_flag_op
)
2452 case FILTER_FLAG_OP_EST
:
2453 p
+= sprintf(p
, " established");
2456 case FILTER_FLAG_OP_ANY
:
2457 case FILTER_FLAG_OP_ALL
:
2458 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2459 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2460 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2461 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2462 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2463 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2464 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2465 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2466 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2467 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2468 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2469 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2470 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2476 p
+= sprintf(p
, " fragments");
2481 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2483 static ip_filter_rulet rule
;
2484 struct in_addr addr
;
2488 if (CLI_HELP_REQUESTED
)
2492 cli_arg_help(cli
, 0,
2493 "ip", "Match IP packets",
2494 "tcp", "Match TCP packets",
2495 "udp", "Match UDP packets",
2501 // *sigh*, too darned complex
2502 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2508 cli_print(cli
, "Specify rule details");
2512 memset(&rule
, 0, sizeof(rule
));
2513 rule
.action
= (command
[0] == 'p')
2514 ? FILTER_ACTION_PERMIT
2515 : FILTER_ACTION_DENY
;
2517 if (MATCH("ip", argv
[0]))
2518 rule
.proto
= IPPROTO_IP
;
2519 else if (MATCH("udp", argv
[0]))
2520 rule
.proto
= IPPROTO_UDP
;
2521 else if (MATCH("tcp", argv
[0]))
2522 rule
.proto
= IPPROTO_TCP
;
2525 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2529 for (a
= 1, i
= 0; i
< 2; i
++)
2533 ip_filter_portt
*port
;
2538 wild
= &rule
.src_wild
;
2539 port
= &rule
.src_ports
;
2544 wild
= &rule
.dst_wild
;
2545 port
= &rule
.dst_ports
;
2548 cli_print(cli
, "Specify destination");
2553 if (MATCH("any", argv
[a
]))
2556 *wild
= INADDR_BROADCAST
;
2559 else if (MATCH("host", argv
[a
]))
2563 cli_print(cli
, "Specify host ip address");
2567 if (!inet_aton(argv
[a
], &addr
))
2569 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2581 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2585 if (!inet_aton(argv
[a
], &addr
))
2587 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2593 if (!inet_aton(argv
[++a
], &addr
))
2595 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2599 *wild
= addr
.s_addr
;
2603 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2607 if (MATCH("eq", argv
[a
]))
2608 port
->op
= FILTER_PORT_OP_EQ
;
2609 else if (MATCH("neq", argv
[a
]))
2610 port
->op
= FILTER_PORT_OP_NEQ
;
2611 else if (MATCH("gt", argv
[a
]))
2612 port
->op
= FILTER_PORT_OP_GT
;
2613 else if (MATCH("lt", argv
[a
]))
2614 port
->op
= FILTER_PORT_OP_LT
;
2615 else if (MATCH("range", argv
[a
]))
2616 port
->op
= FILTER_PORT_OP_RANGE
;
2623 cli_print(cli
, "Specify port");
2627 if (!(port
->port
= atoi(argv
[a
])))
2629 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2634 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2639 cli_print(cli
, "Specify port");
2643 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2645 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2652 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2654 if (MATCH("established", argv
[a
]))
2656 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2659 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2660 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2662 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2663 ? FILTER_FLAG_OP_ANY
2664 : FILTER_FLAG_OP_ALL
;
2668 cli_print(cli
, "Specify tcp flags");
2672 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2676 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2678 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2679 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2680 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2681 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2682 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2683 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2686 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2695 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2697 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2699 cli_print(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2709 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2716 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2718 static ip_filter_rulet rule
;
2719 struct in_addr addr
;
2721 if (CLI_HELP_REQUESTED
)
2725 cli_arg_help(cli
, argv
[0][1],
2726 "A.B.C.D", "Source address",
2727 "any", "Any source address",
2728 "host", "Source host",
2734 if (MATCH("any", argv
[0]))
2736 if (argc
== 2 && !argv
[1][1])
2737 cli_arg_help(cli
, 1, NULL
);
2739 else if (MATCH("host", argv
[0]))
2743 cli_arg_help(cli
, argv
[1][1],
2744 "A.B.C.D", "Host address",
2747 else if (argc
== 3 && !argv
[2][1])
2748 cli_arg_help(cli
, 1, NULL
);
2754 cli_arg_help(cli
, 1,
2755 "A.B.C.D", "Wildcard bits",
2758 else if (argc
== 3 && !argv
[2][1])
2759 cli_arg_help(cli
, 1, NULL
);
2767 cli_print(cli
, "Specify rule details");
2771 memset(&rule
, 0, sizeof(rule
));
2772 rule
.action
= (command
[0] == 'p')
2773 ? FILTER_ACTION_PERMIT
2774 : FILTER_ACTION_DENY
;
2776 rule
.proto
= IPPROTO_IP
;
2777 if (MATCH("any", argv
[0]))
2779 rule
.src_ip
= INADDR_ANY
;
2780 rule
.src_wild
= INADDR_BROADCAST
;
2782 else if (MATCH("host", argv
[0]))
2786 cli_print(cli
, "Specify host ip address");
2790 if (!inet_aton(argv
[1], &addr
))
2792 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2796 rule
.src_ip
= addr
.s_addr
;
2797 rule
.src_wild
= INADDR_ANY
;
2803 cli_print(cli
, "Specify source ip address and wildcard");
2807 if (!inet_aton(argv
[0], &addr
))
2809 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2813 rule
.src_ip
= addr
.s_addr
;
2817 if (!inet_aton(argv
[1], &addr
))
2819 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2823 rule
.src_wild
= addr
.s_addr
;
2826 rule
.src_wild
= INADDR_ANY
;
2832 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2835 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2836 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2837 : access_list_rule_std(cli
, command
, argv
, argc
);
2842 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2844 if (!ip_filters
[filt
].rules
[i
].action
)
2846 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2850 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2854 cli_print(cli
, "Too many rules");
2858 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2863 /* filter USER {in|out} FILTER ... */
2864 if (CLI_HELP_REQUESTED
)
2869 return cli_arg_help(cli
, 0,
2870 "USER", "Username of session to filter", NULL
);
2874 return cli_arg_help(cli
, 0,
2875 "in", "Set incoming filter",
2876 "out", "Set outgoing filter", NULL
);
2880 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2881 "NAME", "Filter name", NULL
);
2884 return cli_arg_help(cli
, argc
> 1, NULL
);
2888 if (!config
->cluster_iam_master
)
2890 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2891 fmtaddr(config
->cluster_master_address
, 0));
2896 if (argc
!= 3 && argc
!= 5)
2898 cli_print(cli
, "Specify a user and filters");
2902 if (!(s
= sessionbyuser(argv
[0])))
2904 cli_print(cli
, "User %s is not connected", argv
[0]);
2908 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2909 for (i
= 1; i
< argc
; i
+= 2)
2914 if (MATCH("in", argv
[i
]))
2916 if (session
[s
].filter_in
)
2918 cli_print(cli
, "Input already filtered");
2921 f
= &cli_session_actions
[s
].filter_in
;
2923 else if (MATCH("out", argv
[i
]))
2925 if (session
[s
].filter_out
)
2927 cli_print(cli
, "Output already filtered");
2930 f
= &cli_session_actions
[s
].filter_out
;
2934 cli_print(cli
, "Invalid filter specification");
2938 v
= find_access_list(argv
[i
+1]);
2939 if (v
< 0 || !*ip_filters
[v
].name
)
2941 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2948 cli_print(cli
, "Filtering user %s", argv
[0]);
2949 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2954 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2959 if (CLI_HELP_REQUESTED
)
2960 return cli_arg_help(cli
, argc
> 1,
2961 "USER", "Username of session to remove filters from", NULL
);
2963 if (!config
->cluster_iam_master
)
2965 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2966 fmtaddr(config
->cluster_master_address
, 0));
2973 cli_print(cli
, "Specify a user to remove filters from");
2977 for (i
= 0; i
< argc
; i
++)
2979 if (!(s
= sessionbyuser(argv
[i
])))
2981 cli_print(cli
, "User %s is not connected", argv
[i
]);
2985 if (session
[s
].filter_in
|| session
[s
].filter_out
)
2987 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
2988 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
2992 cli_print(cli
, "User %s not filtered", argv
[i
]);
2999 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3003 if (CLI_HELP_REQUESTED
)
3004 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3008 cli_print(cli
, "Specify a filter name");
3012 for (i
= 0; i
< argc
; i
++)
3014 int f
= find_access_list(argv
[i
]);
3015 ip_filter_rulet
*rules
;
3017 if (f
< 0 || !*ip_filters
[f
].name
)
3019 cli_print(cli
, "Access-list %s not defined", argv
[i
]);
3026 cli_print(cli
, "%s IP access list %s",
3027 ip_filters
[f
].extended
? "Extended" : "Standard",
3028 ip_filters
[f
].name
);
3030 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3032 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3034 cli_print(cli
, "%s (%d match%s)", r
,
3035 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3037 cli_print(cli
, "%s", r
);
3044 // Convert a string in the form of abcd.ef12.3456 into char[6]
3045 void parsemac(char *string
, char mac
[6])
3047 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)
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)