1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.32 2004/11/28 02:53:11 bodea Exp $";
17 #include <arpa/inet.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
35 extern tunnelt
*tunnel
;
36 extern sessiont
*session
;
37 extern radiust
*radius
;
38 extern ippoolt
*ip_address_pool
;
39 extern struct Tstats
*_statistics
;
40 static struct cli_def
*cli
= NULL
;
41 extern configt
*config
;
42 extern config_descriptt config_values
[];
44 extern struct Tringbuffer
*ringbuffer
;
46 extern struct cli_session_actions
*cli_session_actions
;
47 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
48 extern tbft
*filter_list
;
49 extern ip_filtert
*ip_filters
;
51 static char *debug_levels
[] = {
70 static int debug_session
;
71 static int debug_tunnel
;
72 static int debug_rb_tail
;
74 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
75 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int regular_stuff(struct cli_def
*cli
);
101 static void parsemac(char *string
, char mac
[6]);
104 #define MODE_CONFIG_BGP 8
105 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
106 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
107 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
110 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
111 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
114 #define MODE_CONFIG_NACL 9
115 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
116 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
121 /* match if b is a substr of a */
122 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
124 void init_cli(char *hostname
)
128 struct cli_command
*c
;
129 struct cli_command
*c2
;
131 struct sockaddr_in addr
;
134 if (hostname
&& *hostname
)
135 cli_set_hostname(cli
, hostname
);
137 cli_set_hostname(cli
, "l2tpns");
139 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
140 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
142 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
144 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
145 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
146 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
147 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
148 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
149 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
150 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
151 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
152 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
153 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
154 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");
155 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
157 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
158 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
159 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
162 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
164 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
165 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
168 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
170 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
171 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
172 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
174 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
175 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
176 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
177 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
180 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
181 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
184 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
185 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
186 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
187 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
188 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
191 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
192 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
194 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
195 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
197 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
198 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
200 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
202 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
203 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
206 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
207 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
208 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
209 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
211 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
212 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
214 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
215 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
217 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
219 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
220 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
222 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
223 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
225 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
226 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
227 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
229 // Enable regular processing
230 cli_regular(cli
, regular_stuff
);
232 if (!(f
= fopen(CLIUSERS
, "r")))
234 LOG(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
238 while (fgets(buf
, 4096, f
))
241 if (*buf
== '#') continue;
242 if ((p
= strchr(buf
, '\r'))) *p
= 0;
243 if ((p
= strchr(buf
, '\n'))) *p
= 0;
245 if (!(p
= strchr((char *)buf
, ':'))) continue;
247 if (!strcmp(buf
, "enable"))
249 cli_allow_enable(cli
, p
);
250 LOG(3, 0, 0, 0, "Setting enable password\n");
254 cli_allow_user(cli
, buf
, p
);
255 LOG(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
261 memset(&addr
, 0, sizeof(addr
));
262 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
263 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
266 // Set cli fd as non-blocking
267 flags
= fcntl(clifd
, F_GETFL
, 0);
268 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
270 addr
.sin_family
= AF_INET
;
271 addr
.sin_port
= htons(23);
272 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
274 LOG(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
280 void cli_do(int sockfd
)
282 int require_auth
= 1;
283 struct sockaddr_in addr
;
284 int l
= sizeof(addr
);
286 if (fork_and_close()) return;
287 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
289 LOG(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
290 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
293 LOG(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
297 LOG(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
298 if (!cli
->users
) /* paranoia */
300 LOG(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
306 /* no username/pass required */
313 debug_rb_tail
= ringbuffer
->tail
;
315 memset(&debug_flags
, 0, sizeof(debug_flags
));
316 debug_flags
.critical
= 1;
318 cli_loop(cli
, sockfd
);
321 LOG(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
325 static void cli_print_log(struct cli_def
*cli
, char *string
)
327 LOG(3, 0, 0, 0, "%s\n", string
);
330 void cli_do_file(FILE *fh
)
332 LOG(3, 0, 0, 0, "Reading configuration file\n");
333 cli_print_callback(cli
, cli_print_log
);
334 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
335 cli_print_callback(cli
, NULL
);
338 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
349 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
351 int v
= va_arg(ap
, int);
352 snprintf(buf
, sizeof(buf
), entry
, v
);
358 desc
= va_arg(ap
, char *);
360 cli_print(cli
, " %-20s %s", p
, desc
);
362 cli_print(cli
, " %s", p
);
364 entry
= desc
? va_arg(ap
, char *) : 0;
369 cli_print(cli
, " <cr>");
374 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
378 if (CLI_HELP_REQUESTED
)
379 return cli_arg_help(cli
, 1,
380 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
386 // Show individual session
387 for (i
= 0; i
< argc
; i
++)
389 unsigned int s
, b_in
, b_out
;
391 if (s
<= 0 || s
>= MAXSESSION
)
393 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
396 cli_print(cli
, "\r\nSession %d:", s
);
397 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
398 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
399 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
400 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
401 cli_print(cli
, "\tIP address:\t%s", inet_toa(htonl(session
[s
].ip
)));
402 cli_print(cli
, "\tUnique SID:\t%lu", session
[s
].unique_id
);
403 cli_print(cli
, "\tIdle time:\t%u seconds", abs(time_now
- session
[s
].last_packet
));
404 cli_print(cli
, "\tNext Recv:\t%u", session
[s
].nr
);
405 cli_print(cli
, "\tNext Send:\t%u", session
[s
].ns
);
406 cli_print(cli
, "\tBytes In/Out:\t%lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
407 cli_print(cli
, "\tPkts In/Out:\t%lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
408 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
409 cli_print(cli
, "\tRadius Session:\t%u", session
[s
].radius
);
410 cli_print(cli
, "\tRx Speed:\t%lu", session
[s
].rx_connect_speed
);
411 cli_print(cli
, "\tTx Speed:\t%lu", session
[s
].tx_connect_speed
);
412 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
413 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
414 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
415 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
416 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
417 cli_print(cli
, "\tIntercepted:\t%s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
419 cli_print(cli
, "\tIntercepted:\tno");
421 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
423 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
424 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
425 t
? "YES" : "no", t
? " (" : "",
426 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
428 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
432 b_in
= session
[s
].tbf_in
;
433 b_out
= session
[s
].tbf_out
;
435 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
436 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
437 "ByteSent", "PackSent", "PackDrop", "PackDelay");
440 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
442 (filter_list
[b_in
].next
? "*" : " "),
443 (b_in
< 100 ? "\t" : ""),
444 filter_list
[b_in
].rate
* 8,
445 filter_list
[b_in
].credit
,
446 filter_list
[b_in
].queued
,
447 filter_list
[b_in
].b_queued
,
448 filter_list
[b_in
].p_queued
,
449 filter_list
[b_in
].b_sent
,
450 filter_list
[b_in
].p_sent
,
451 filter_list
[b_in
].p_dropped
,
452 filter_list
[b_in
].p_delayed
);
455 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
457 (filter_list
[b_out
].next
? "*" : " "),
458 (b_out
< 100 ? "\t" : ""),
459 filter_list
[b_out
].rate
* 8,
460 filter_list
[b_out
].credit
,
461 filter_list
[b_out
].queued
,
462 filter_list
[b_out
].b_queued
,
463 filter_list
[b_out
].p_queued
,
464 filter_list
[b_out
].b_sent
,
465 filter_list
[b_out
].p_sent
,
466 filter_list
[b_out
].p_dropped
,
467 filter_list
[b_out
].p_delayed
);
474 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
489 for (i
= 1; i
< MAXSESSION
; i
++)
491 char *userip
, *tunnelip
;
492 if (!session
[i
].opened
) continue;
493 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
494 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
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
: "*",
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
)),
508 session
[i
].calling
[0] ? session
[i
].calling
: "*");
509 if (userip
) free(userip
);
510 if (tunnelip
) free(tunnelip
);
515 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
517 int i
, x
, show_all
= 0;
525 if (CLI_HELP_REQUESTED
)
528 return cli_arg_help(cli
, 1,
529 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
532 return cli_arg_help(cli
, 1,
533 "all", "Show all tunnels, including unused",
534 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
541 if (strcmp(argv
[0], "all") == 0)
547 // Show individual tunnel
548 for (i
= 0; i
< argc
; i
++)
553 if (t
<= 0 || t
>= MAXTUNNEL
)
555 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
558 cli_print(cli
, "\r\nTunnel %d:", t
);
559 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
560 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
561 cli_print(cli
, "\tRemote IP:\t%s", inet_toa(htonl(tunnel
[t
].ip
)));
562 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
563 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
564 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
565 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
566 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
567 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
569 for (x
= 0; x
< MAXSESSION
; x
++)
570 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
571 sprintf(s
, "%s%u ", s
, x
);
573 cli_print(cli
, "\tSessions:\t%s", s
);
579 // Show tunnel summary
580 cli_print(cli
, "%4s %20s %20s %6s %s",
587 for (i
= 1; i
< MAXTUNNEL
; i
++)
590 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
592 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
593 cli_print(cli
, "%4d %20s %20s %6s %6d",
595 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
596 inet_toa(htonl(tunnel
[i
].ip
)),
597 states
[tunnel
[i
].state
],
604 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
611 if (CLI_HELP_REQUESTED
)
612 return cli_arg_help(cli
, 1,
613 "USER", "Show details for specific username",
616 for (i
= 0; i
< MAXSESSION
; i
++)
618 if (!session
[i
].opened
) continue;
619 if (!session
[i
].user
[0]) continue;
623 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
625 if (strcmp(argv
[j
], session
[i
].user
) == 0)
627 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
628 sargv
[sargc
] = sid
[sargc
];
636 cli_print(cli
, "%s", session
[i
].user
);
640 return cmd_show_session(cli
, "users", sargv
, sargc
);
645 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
647 if (CLI_HELP_REQUESTED
)
648 return CLI_HELP_NO_ARGS
;
650 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
651 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
652 GET_STAT(tun_rx_bytes
),
653 GET_STAT(tun_rx_packets
),
654 GET_STAT(tun_rx_errors
));
655 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
656 GET_STAT(tun_tx_bytes
),
657 GET_STAT(tun_tx_packets
),
658 GET_STAT(tun_tx_errors
));
661 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
662 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
663 GET_STAT(tunnel_rx_bytes
),
664 GET_STAT(tunnel_rx_packets
),
665 GET_STAT(tunnel_rx_errors
),
667 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
668 GET_STAT(tunnel_tx_bytes
),
669 GET_STAT(tunnel_tx_packets
),
670 GET_STAT(tunnel_tx_errors
),
671 GET_STAT(tunnel_retries
));
674 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
675 cli_print(cli
, "-----------------------------------------");
676 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
677 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
678 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
679 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
680 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
681 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
682 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
683 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
684 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
685 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
686 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
687 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
688 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
689 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
690 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
694 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
695 cli_print(cli
, "-----------------------------------------");
696 cli_print(cli
, "%-30s%lu", "call_processtun", GET_STAT(call_processtun
));
697 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
698 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
699 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
700 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
701 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
702 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
703 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
704 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
705 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
706 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
707 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
708 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
709 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
710 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
711 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
712 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
713 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
714 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
715 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
716 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
717 cli_print(cli
, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
718 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
719 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
720 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
721 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
726 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
732 if (CLI_HELP_REQUESTED
)
733 return cli_arg_help(cli
, 1,
734 "tag", "Include CVS release tag",
735 "file", "Include file versions",
738 for (i
= 0; i
< argc
; i
++)
739 if (!strcmp(argv
[i
], "tag"))
741 else if (!strcmp(argv
[i
], "file"))
744 cli_print(cli
, "L2TPNS %s", VERSION
);
747 char const *p
= strchr(cvs_name
, ':');
759 e
= strpbrk(p
, " \t$");
760 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
765 extern linked_list
*loaded_plugins
;
768 cli_print(cli
, "Files:");
769 cli_print(cli
, " %s", cvs_id_arp
);
771 cli_print(cli
, " %s", cvs_id_bgp
);
773 cli_print(cli
, " %s", cvs_id_cli
);
774 cli_print(cli
, " %s", cvs_id_cluster
);
775 cli_print(cli
, " %s", cvs_id_constants
);
776 cli_print(cli
, " %s", cvs_id_control
);
777 cli_print(cli
, " %s", cvs_id_icmp
);
778 cli_print(cli
, " %s", cvs_id_l2tpns
);
779 cli_print(cli
, " %s", cvs_id_ll
);
780 cli_print(cli
, " %s", cvs_id_md5
);
781 cli_print(cli
, " %s", cvs_id_ppp
);
782 cli_print(cli
, " %s", cvs_id_radius
);
783 cli_print(cli
, " %s", cvs_id_tbf
);
784 cli_print(cli
, " %s", cvs_id_util
);
786 ll_reset(loaded_plugins
);
787 while ((p
= ll_next(loaded_plugins
)))
789 char const **id
= dlsym(p
, "cvs_id");
791 cli_print(cli
, " %s", *id
);
798 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
801 int used
= 0, free
= 0, show_all
= 0;
803 if (!config
->cluster_iam_master
)
805 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
809 if (CLI_HELP_REQUESTED
)
812 return cli_arg_help(cli
, 1, NULL
);
814 return cli_arg_help(cli
, 1,
815 "all", "Show all pool addresses, including unused",
819 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
823 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
824 for (i
= 0; i
< MAXIPPOOL
; i
++)
826 if (!ip_address_pool
[i
].address
) continue;
827 if (ip_address_pool
[i
].assigned
)
829 cli_print(cli
, "%-15s\tY %8d %s",
830 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
836 if (ip_address_pool
[i
].last
)
837 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
838 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
839 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
841 cli_print(cli
, "%-15s\tN", inet_toa(htonl(ip_address_pool
[i
].address
)));
848 cli_print(cli
, "(Not displaying unused addresses)");
850 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
854 static FILE *save_config_fh
= 0;
855 static void print_save_config(struct cli_def
*cli
, char *string
)
858 fprintf(save_config_fh
, "%s\n", string
);
861 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
863 if (CLI_HELP_REQUESTED
)
864 return CLI_HELP_NO_ARGS
;
866 if ((save_config_fh
= fopen(config
->config_file
, "w")))
868 cli_print(cli
, "Writing configuration");
869 cli_print_callback(cli
, print_save_config
);
870 cmd_show_run(cli
, command
, argv
, argc
);
871 cli_print_callback(cli
, NULL
);
872 fclose(save_config_fh
);
877 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
882 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
884 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
888 if (CLI_HELP_REQUESTED
)
889 return CLI_HELP_NO_ARGS
;
891 cli_print(cli
, "# Current configuration:");
893 for (i
= 0; config_values
[i
].key
; i
++)
895 void *value
= ((void *)config
) + config_values
[i
].offset
;
896 if (config_values
[i
].type
== STRING
)
897 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
898 else if (config_values
[i
].type
== IP
)
899 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
900 else if (config_values
[i
].type
== SHORT
)
901 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
902 else if (config_values
[i
].type
== BOOL
)
903 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
904 else if (config_values
[i
].type
== INT
)
905 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
906 else if (config_values
[i
].type
== UNSIGNED_LONG
)
907 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
908 else if (config_values
[i
].type
== MAC
)
909 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
910 *(unsigned short *)(value
+ 0),
911 *(unsigned short *)(value
+ 1),
912 *(unsigned short *)(value
+ 2),
913 *(unsigned short *)(value
+ 3),
914 *(unsigned short *)(value
+ 4),
915 *(unsigned short *)(value
+ 5));
918 cli_print(cli
, "# Plugins");
919 for (i
= 0; i
< MAXPLUGINS
; i
++)
921 if (*config
->plugins
[i
])
923 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
928 if (config
->as_number
)
933 cli_print(cli
, "# BGP");
934 cli_print(cli
, "router bgp %u", config
->as_number
);
935 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
937 if (!config
->neighbour
[i
].name
[0])
940 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
942 k
= config
->neighbour
[i
].keepalive
;
943 h
= config
->neighbour
[i
].hold
;
950 k
= BGP_KEEPALIVE_TIME
;
956 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
961 cli_print(cli
, "# Filters");
962 for (i
= 0; i
< MAXFILTER
; i
++)
964 ip_filter_rulet
*rules
;
965 if (!*ip_filters
[i
].name
)
968 cli_print(cli
, "ip access-list %s %s",
969 ip_filters
[i
].extended
? "extended" : "standard",
972 rules
= ip_filters
[i
].rules
;
973 while (rules
->action
)
974 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
977 cli_print(cli
, "# end");
981 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
983 int i
, free
= 0, used
= 0, show_all
= 0;
994 if (CLI_HELP_REQUESTED
)
997 return cli_arg_help(cli
, 1, NULL
);
999 return cli_arg_help(cli
, 1,
1000 "all", "Show all RADIUS sessions, including unused",
1004 cli_print(cli
, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1008 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1011 for (i
= 1; i
< MAXRADIUS
; i
++)
1013 if (radius
[i
].state
== RADIUSNULL
)
1018 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1020 cli_print(cli
, "%6d%6d%5d%6s%9d%9u%4d",
1024 states
[radius
[i
].state
],
1030 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1035 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1039 if (CLI_HELP_REQUESTED
)
1040 return CLI_HELP_NO_ARGS
;
1042 cli_print(cli
, "Plugins currently loaded:");
1043 for (i
= 0; i
< MAXPLUGINS
; i
++)
1044 if (*config
->plugins
[i
])
1045 cli_print(cli
, " %s", config
->plugins
[i
]);
1050 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1054 if (CLI_HELP_REQUESTED
)
1055 return CLI_HELP_NO_ARGS
;
1057 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1066 for (i
= 0; i
< MAXSESSION
; i
++)
1068 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1069 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1073 session
[i
].throttle_in
,
1074 session
[i
].throttle_out
,
1076 session
[i
].tbf_out
);
1082 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1084 if (CLI_HELP_REQUESTED
)
1085 return CLI_HELP_NO_ARGS
;
1087 cli_print(cli
, " _\n"
1093 " ( \\ `. `-. _,.-:\\\n"
1094 " \\ \\ `. `-._ __..--' ,-';/\n"
1095 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1096 " `. `. `-._ __..--' ,' /\n"
1097 " `. `-_ ``--..'' _.-' ,'\n"
1098 " `-_ `-.___ __,--' ,'\n"
1099 " `-.__ `----\"\"\" __.-'\n"
1100 "hh `--..____..--'");
1105 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1107 if (CLI_HELP_REQUESTED
)
1108 return CLI_HELP_NO_ARGS
;
1110 cli_print(cli
, "Counters cleared");
1111 SET_STAT(last_reset
, time(NULL
));
1115 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1120 if (CLI_HELP_REQUESTED
)
1121 return cli_arg_help(cli
, argc
> 1,
1122 "USER", "Username of session to drop", NULL
);
1124 if (!config
->cluster_iam_master
)
1126 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1132 cli_print(cli
, "Specify a user to drop");
1136 for (i
= 0; i
< argc
; i
++)
1138 if (!(s
= sessionbyuser(argv
[i
])))
1140 cli_print(cli
, "User %s is not connected", argv
[i
]);
1144 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1146 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1147 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1154 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1159 if (CLI_HELP_REQUESTED
)
1160 return cli_arg_help(cli
, argc
> 1,
1161 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1163 if (!config
->cluster_iam_master
)
1165 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1171 cli_print(cli
, "Specify a tunnel to drop");
1175 for (i
= 0; i
< argc
; i
++)
1177 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1179 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1185 cli_print(cli
, "Tunnel %d is not connected", t
);
1191 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1195 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1196 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1202 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1207 if (CLI_HELP_REQUESTED
)
1208 return cli_arg_help(cli
, argc
> 1,
1209 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1211 if (!config
->cluster_iam_master
)
1213 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1219 cli_print(cli
, "Specify a session id to drop");
1223 for (i
= 0; i
< argc
; i
++)
1225 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1227 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1231 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1233 cli_print(cli
, "Dropping session %d", s
);
1234 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1238 cli_print(cli
, "Session %d is not active.", s
);
1245 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1251 if (CLI_HELP_REQUESTED
)
1256 return cli_arg_help(cli
, 0,
1257 "USER", "Username of session to snoop", NULL
);
1260 return cli_arg_help(cli
, 0,
1261 "A.B.C.D", "IP address of snoop destination", NULL
);
1264 return cli_arg_help(cli
, 0,
1265 "N", "Port of snoop destination", NULL
);
1269 return cli_arg_help(cli
, 1, NULL
);
1276 if (!config
->cluster_iam_master
)
1278 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1284 cli_print(cli
, "Specify username, ip and port");
1288 if (!(s
= sessionbyuser(argv
[0])))
1290 cli_print(cli
, "User %s is not connected", argv
[0]);
1294 ip
= inet_addr(argv
[1]);
1295 if (!ip
|| ip
== INADDR_NONE
)
1297 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1301 port
= atoi(argv
[2]);
1304 cli_print(cli
, "Invalid port %s", argv
[2]);
1308 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(ip
), port
);
1309 cli_session_actions
[s
].snoop_ip
= ip
;
1310 cli_session_actions
[s
].snoop_port
= port
;
1311 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1316 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1321 if (CLI_HELP_REQUESTED
)
1322 return cli_arg_help(cli
, argc
> 1,
1323 "USER", "Username of session to unsnoop", NULL
);
1325 if (!config
->cluster_iam_master
)
1327 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1333 cli_print(cli
, "Specify a user to unsnoop");
1337 for (i
= 0; i
< argc
; i
++)
1339 if (!(s
= sessionbyuser(argv
[i
])))
1341 cli_print(cli
, "User %s is not connected", argv
[i
]);
1345 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1346 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1352 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1359 throttle USER - throttle in/out to default rate
1360 throttle USER RATE - throttle in/out to default rate
1361 throttle USER in RATE - throttle input only
1362 throttle USER out RATE - throttle output only
1363 throttle USER in RATE out RATE - throttle both
1366 if (CLI_HELP_REQUESTED
)
1371 return cli_arg_help(cli
, 0,
1372 "USER", "Username of session to throttle", NULL
);
1375 return cli_arg_help(cli
, 1,
1376 "RATE", "Rate in kbps (in and out)",
1377 "in", "Select incoming rate",
1378 "out", "Select outgoing rate", NULL
);
1381 return cli_arg_help(cli
, 1,
1382 "in", "Select incoming rate",
1383 "out", "Select outgoing rate", NULL
);
1386 if (isdigit(argv
[1][0]))
1387 return cli_arg_help(cli
, 1, NULL
);
1390 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1393 return cli_arg_help(cli
, argc
> 1, NULL
);
1397 if (!config
->cluster_iam_master
)
1399 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1405 cli_print(cli
, "Specify a user to throttle");
1409 if (!(s
= sessionbyuser(argv
[0])))
1411 cli_print(cli
, "User %s is not connected", argv
[0]);
1417 rate_in
= rate_out
= config
->rl_rate
;
1421 rate_in
= rate_out
= atoi(argv
[1]);
1424 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1428 else if (argc
== 3 || argc
== 5)
1431 for (i
= 1; i
< argc
- 1; i
+= 2)
1434 if (MATCH("in", argv
[i
]))
1435 r
= rate_in
= atoi(argv
[i
+1]);
1436 else if (MATCH("out", argv
[i
]))
1437 r
= rate_out
= atoi(argv
[i
+1]);
1441 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1448 cli_print(cli
, "Invalid arguments");
1452 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1454 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1458 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1459 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1460 cli_session_actions
[s
].throttle_in
= rate_in
;
1462 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1463 cli_session_actions
[s
].throttle_out
= rate_out
;
1465 if (cli_session_actions
[s
].throttle_in
== -1 &&
1466 cli_session_actions
[s
].throttle_out
== -1)
1468 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1472 cli_print(cli
, "Throttling user %s", argv
[0]);
1473 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1478 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1483 if (CLI_HELP_REQUESTED
)
1484 return cli_arg_help(cli
, argc
> 1,
1485 "USER", "Username of session to unthrottle", NULL
);
1487 if (!config
->cluster_iam_master
)
1489 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1495 cli_print(cli
, "Specify a user to unthrottle");
1499 for (i
= 0; i
< argc
; i
++)
1501 if (!(s
= sessionbyuser(argv
[i
])))
1503 cli_print(cli
, "User %s is not connected", argv
[i
]);
1507 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1509 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1510 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1514 cli_print(cli
, "User %s not throttled", argv
[i
]);
1521 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1525 if (CLI_HELP_REQUESTED
)
1526 return cli_arg_help(cli
, 1,
1527 "all", "Enable debugging for all except \"data\"",
1528 "critical", "", // FIXME: add descriptions
1538 char *p
= (char *) &debug_flags
;
1539 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1543 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1544 (debug_flags
.critical
) ? " critical" : "",
1545 (debug_flags
.error
) ? " error" : "",
1546 (debug_flags
.warning
) ? " warning" : "",
1547 (debug_flags
.info
) ? " info" : "",
1548 (debug_flags
.calls
) ? " calls" : "",
1549 (debug_flags
.data
) ? " data" : "");
1555 cli_print(cli
, "Debugging off");
1559 for (i
= 0; i
< argc
; i
++)
1561 int len
= strlen(argv
[i
]);
1563 if (argv
[i
][0] == 'c' && len
< 2)
1564 len
= 2; /* distinguish [cr]itical from [ca]lls */
1566 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1567 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1568 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1569 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1570 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1571 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1572 if (!strncmp("all", argv
[i
], len
))
1574 memset(&debug_flags
, 1, sizeof(debug_flags
));
1575 debug_flags
.data
= 0;
1579 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1585 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1589 if (CLI_HELP_REQUESTED
)
1590 return cli_arg_help(cli
, 1,
1591 "all", "Disable all debugging",
1592 "critical", "", // FIXME: add descriptions
1602 memset(&debug_flags
, 0, sizeof(debug_flags
));
1606 for (i
= 0; i
< argc
; i
++)
1608 int len
= strlen(argv
[i
]);
1610 if (argv
[i
][0] == 'c' && len
< 2)
1611 len
= 2; /* distinguish [cr]itical from [ca]lls */
1613 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1614 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1615 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1616 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1617 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1618 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1619 if (!strncmp("all", argv
[i
], len
))
1621 memset(&debug_flags
, 0, sizeof(debug_flags
));
1625 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1631 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1633 int i
, firstfree
= 0;
1635 if (CLI_HELP_REQUESTED
)
1636 return cli_arg_help(cli
, argc
> 1,
1637 "PLUGIN", "Name of plugin to load", NULL
);
1641 cli_print(cli
, "Specify a plugin to load");
1645 for (i
= 0; i
< MAXPLUGINS
; i
++)
1647 if (!*config
->plugins
[i
] && !firstfree
)
1649 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1651 cli_print(cli
, "Plugin is already loaded");
1658 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1659 config
->reload_config
= 1;
1660 cli_print(cli
, "Loading plugin %s", argv
[0]);
1666 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1670 if (CLI_HELP_REQUESTED
)
1671 return cli_arg_help(cli
, argc
> 1,
1672 "PLUGIN", "Name of plugin to unload", NULL
);
1676 cli_print(cli
, "Specify a plugin to remove");
1680 for (i
= 0; i
< MAXPLUGINS
; i
++)
1682 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1684 config
->reload_config
= 1;
1685 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1690 cli_print(cli
, "Plugin is not loaded");
1694 static char *duration(time_t secs
)
1696 static char *buf
= NULL
;
1699 if (!buf
) buf
= calloc(64, 1);
1703 int days
= secs
/ 86400;
1704 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1710 int mins
= secs
/ 60;
1711 int hrs
= mins
/ 60;
1714 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1716 else if (secs
>= 60)
1718 int mins
= secs
/ 60;
1719 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1722 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1727 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1730 char buf
[100], *p
= buf
, *loads
[3];
1731 int i
, num_sessions
= 0;
1733 if (CLI_HELP_REQUESTED
)
1734 return CLI_HELP_NO_ARGS
;
1736 fh
= fopen("/proc/loadavg", "r");
1737 fgets(buf
, 100, fh
);
1740 for (i
= 0; i
< 3; i
++)
1741 loads
[i
] = strdup(strsep(&p
, " "));
1744 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1746 for (i
= 1; i
< MAXSESSION
; i
++)
1747 if (session
[i
].opened
) num_sessions
++;
1749 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1751 duration(time_now
- config
->start_time
),
1753 loads
[0], loads
[1], loads
[2]
1755 for (i
= 0; i
< 3; i
++)
1756 if (loads
[i
]) free(loads
[i
]);
1758 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1763 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1767 if (CLI_HELP_REQUESTED
)
1773 int len
= strlen(argv
[0])-1;
1774 for (i
= 0; config_values
[i
].key
; i
++)
1775 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1776 cli_print(cli
, " %s", config_values
[i
].key
);
1782 return cli_arg_help(cli
, 0,
1783 "VALUE", "Value for variable", NULL
);
1787 return cli_arg_help(cli
, 1, NULL
);
1796 cli_print(cli
, "Specify variable and value");
1800 for (i
= 0; config_values
[i
].key
; i
++)
1802 void *value
= ((void *)config
) + config_values
[i
].offset
;
1803 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1805 // Found a value to set
1806 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1807 switch (config_values
[i
].type
)
1810 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1813 *(int *)value
= atoi(argv
[1]);
1816 *(unsigned long *)value
= atol(argv
[1]);
1819 *(short *)value
= atoi(argv
[1]);
1822 *(unsigned *)value
= inet_addr(argv
[1]);
1825 parsemac(argv
[1], (char *)value
);
1828 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1834 cli_print(cli
, "Unknown variable type");
1837 config
->reload_config
= 1;
1842 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1846 int regular_stuff(struct cli_def
*cli
)
1848 int i
= debug_rb_tail
;
1852 while (i
!= ringbuffer
->tail
)
1854 int show_message
= 0;
1856 if (*ringbuffer
->buffer
[i
].message
)
1858 // Always show messages if we are doing general debug
1859 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1860 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1861 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1862 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1863 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1864 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1869 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1871 struct in_addr addr
;
1873 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1874 ipaddr
= inet_ntoa(addr
);
1876 cli_print(cli
, "\r%s-%s-%u-%u %s",
1877 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1879 ringbuffer
->buffer
[i
].tunnel
,
1880 ringbuffer
->buffer
[i
].session
,
1881 ringbuffer
->buffer
[i
].message
);
1886 if (++i
== ringbuffer
->tail
) break;
1887 if (i
== RINGBUFFER_SIZE
) i
= 0;
1890 debug_rb_tail
= ringbuffer
->tail
;
1898 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1902 if (CLI_HELP_REQUESTED
)
1903 return cli_arg_help(cli
, argc
> 1,
1904 "<1-65535>", "Autonomous system number", NULL
);
1906 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1908 cli_print(cli
, "Invalid autonomous system number");
1912 if (bgp_configured
&& as
!= config
->as_number
)
1914 cli_print(cli
, "Can't change local AS on a running system");
1918 config
->as_number
= as
;
1919 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1924 static int find_bgp_neighbour(char const *name
)
1929 in_addr_t addrs
[4] = { 0 };
1932 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1935 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1936 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1938 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1940 if (!config
->neighbour
[i
].name
[0])
1942 if (new == -1) new = i
;
1946 if (!strcmp(name
, config
->neighbour
[i
].name
))
1949 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1952 for (a
= h
->h_addr_list
; *a
; a
++)
1955 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1956 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1964 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1970 if (CLI_HELP_REQUESTED
)
1975 return cli_arg_help(cli
, 0,
1976 "A.B.C.D", "BGP neighbour address",
1977 "NAME", "BGP neighbour name",
1981 return cli_arg_help(cli
, 0,
1982 "remote-as", "Set remote autonomous system number",
1983 "timers", "Set timers",
1987 if (MATCH("remote-as", argv
[1]))
1988 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
1990 if (MATCH("timers", argv
[1]))
1993 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
1996 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
1998 if (argc
== 5 && !argv
[4][1])
1999 return cli_arg_help(cli
, 1, NULL
);
2008 cli_print(cli
, "Invalid arguments");
2012 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2014 cli_print(cli
, "Invalid neighbour");
2020 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2024 if (MATCH("remote-as", argv
[1]))
2026 int as
= atoi(argv
[2]);
2027 if (as
< 0 || as
> 65535)
2029 cli_print(cli
, "Invalid autonomous system number");
2033 if (!config
->neighbour
[i
].name
[0])
2035 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2036 config
->neighbour
[i
].keepalive
= -1;
2037 config
->neighbour
[i
].hold
= -1;
2040 config
->neighbour
[i
].as
= as
;
2044 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2046 cli_print(cli
, "Invalid arguments");
2050 if (!config
->neighbour
[i
].name
[0])
2052 cli_print(cli
, "Specify remote-as first");
2056 keepalive
= atoi(argv
[2]);
2057 hold
= atoi(argv
[3]);
2059 if (keepalive
< 1 || keepalive
> 65535)
2061 cli_print(cli
, "Invalid keepalive time");
2065 if (hold
< 3 || hold
> 65535)
2067 cli_print(cli
, "Invalid hold time");
2071 if (keepalive
== BGP_KEEPALIVE_TIME
)
2072 keepalive
= -1; // using default value
2074 if (hold
== BGP_HOLD_TIME
)
2077 config
->neighbour
[i
].keepalive
= keepalive
;
2078 config
->neighbour
[i
].hold
= hold
;
2083 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2087 if (CLI_HELP_REQUESTED
)
2088 return cli_arg_help(cli
, argc
> 0,
2089 "A.B.C.D", "BGP neighbour address",
2090 "NAME", "BGP neighbour name",
2095 cli_print(cli
, "Specify a BGP neighbour");
2099 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2101 cli_print(cli
, "Invalid neighbour");
2105 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2107 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2111 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2115 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2121 if (!bgp_configured
)
2124 if (CLI_HELP_REQUESTED
)
2125 return cli_arg_help(cli
, 1,
2126 "A.B.C.D", "BGP neighbour address",
2127 "NAME", "BGP neighbour name",
2130 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2131 BGP_VERSION
, inet_toa(my_address
), (int) config
->as_number
);
2135 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2137 if (!*bgp_peers
[i
].name
)
2140 addr
= inet_toa(bgp_peers
[i
].addr
);
2141 if (argc
&& strcmp(addr
, argv
[0]) &&
2142 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2148 cli_print(cli
, "Peer AS Address "
2149 "State Retries Retry in Route Pend Timers");
2150 cli_print(cli
, "------------------ ----- --------------- "
2151 "----------- ------- -------- ----- ---- ---------");
2154 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s %4d %4d",
2158 bgp_state_str(bgp_peers
[i
].state
),
2159 bgp_peers
[i
].retry_count
,
2160 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2161 bgp_peers
[i
].routing
? "yes" : "no",
2162 bgp_peers
[i
].update_routes
? "yes" : "no",
2163 bgp_peers
[i
].keepalive
,
2170 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2175 if (!bgp_configured
)
2178 if (CLI_HELP_REQUESTED
)
2179 return cli_arg_help(cli
, 1,
2180 "A.B.C.D", "BGP neighbour address",
2181 "NAME", "BGP neighbour name",
2184 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2186 if (bgp_peers
[i
].state
!= Established
)
2189 if (!bgp_peers
[i
].routing
)
2192 addr
= inet_toa(bgp_peers
[i
].addr
);
2193 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2196 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2197 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2203 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2208 if (!bgp_configured
)
2211 if (CLI_HELP_REQUESTED
)
2212 return cli_arg_help(cli
, 1,
2213 "A.B.C.D", "BGP neighbour address",
2214 "NAME", "BGP neighbour name",
2217 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2219 if (bgp_peers
[i
].state
!= Established
)
2222 if (bgp_peers
[i
].routing
)
2225 addr
= inet_toa(bgp_peers
[i
].addr
);
2226 if (argc
&& strcmp(addr
, argv
[0]) &&
2227 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2230 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2231 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2237 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2242 if (!bgp_configured
)
2245 if (CLI_HELP_REQUESTED
)
2246 return cli_arg_help(cli
, 1,
2247 "A.B.C.D", "BGP neighbour address",
2248 "NAME", "BGP neighbour name",
2251 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2253 if (!*bgp_peers
[i
].name
)
2256 addr
= inet_toa(bgp_peers
[i
].addr
);
2257 if (argc
&& strcmp(addr
, argv
[0]) &&
2258 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2261 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2262 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2270 static int find_access_list(char const *name
)
2274 for (i
= 0; i
< MAXFILTER
; i
++)
2275 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2281 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2285 if (CLI_HELP_REQUESTED
)
2290 return cli_arg_help(cli
, 0,
2291 "standard", "Standard syntax",
2292 "extended", "Extended syntax",
2296 return cli_arg_help(cli
, argv
[1][1],
2297 "NAME", "Access-list name",
2301 if (argc
== 3 && !argv
[2][1])
2302 return cli_arg_help(cli
, 1, NULL
);
2310 cli_print(cli
, "Specify access-list type and name");
2314 if (MATCH("standard", argv
[0]))
2316 else if (MATCH("extended", argv
[0]))
2320 cli_print(cli
, "Invalid access-list type");
2324 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2325 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2327 cli_print(cli
, "Invalid access-list name");
2331 filt
= find_access_list(argv
[1]);
2336 cli_print(cli
, "Too many access-lists");
2341 if (!*ip_filters
[filt
].name
)
2343 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2344 strcpy(ip_filters
[filt
].name
, argv
[1]);
2345 ip_filters
[filt
].extended
= extended
;
2347 else if (ip_filters
[filt
].extended
!= extended
)
2349 cli_print(cli
, "Access-list is %s",
2350 ip_filters
[filt
].extended
? "extended" : "standard");
2355 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2359 if (filt
< 0 || !*ip_filters
[filt
].name
)
2361 cli_print(cli
, "Access-list not defined");
2366 if (ip_filters
[filt
].used
)
2368 cli_print(cli
, "Access-list in use");
2372 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2376 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2378 return access_list(cli
, argv
, argc
, 1);
2381 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2383 return access_list(cli
, argv
, argc
, 0);
2386 static int show_ip_wild(char *buf
, ipt ip
, ipt wild
)
2389 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2390 return sprintf(buf
, " any");
2392 if (wild
== INADDR_ANY
)
2393 return sprintf(buf
, " host %s", inet_toa(ip
));
2395 i
= sprintf(buf
, " %s", inet_toa(ip
));
2396 return i
+ sprintf(buf
+ i
, " %s", inet_toa(wild
));
2399 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2403 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2404 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2405 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2406 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2407 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2413 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2415 static char buf
[256];
2418 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2421 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2422 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2425 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2429 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2430 p
+= show_ports(p
, &rule
->src_ports
);
2432 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2433 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2434 p
+= show_ports(p
, &rule
->dst_ports
);
2436 if (rule
->proto
== IPPROTO_TCP
&& (rule
->tcp_sflags
|| rule
->tcp_cflags
))
2438 if (rule
->tcp_flag_op
== FILTER_FLAG_OP_ANY
&&
2439 rule
->tcp_sflags
== (TCP_FLAG_ACK
|TCP_FLAG_FIN
) &&
2440 rule
->tcp_cflags
== TCP_FLAG_SYN
)
2442 p
+= sprintf(p
, " established");
2446 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2447 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2448 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2449 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2450 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2451 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2452 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2453 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2454 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2455 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2456 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2457 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2458 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2465 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2467 static ip_filter_rulet rule
;
2468 struct in_addr addr
;
2472 if (CLI_HELP_REQUESTED
)
2476 cli_arg_help(cli
, 0,
2477 "ip", "Match IP packets",
2478 "tcp", "Match TCP packets",
2479 "udp", "Match UDP packets",
2485 // *sigh*, too darned complex
2486 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2492 cli_print(cli
, "Specify rule details");
2496 memset(&rule
, 0, sizeof(rule
));
2497 rule
.action
= (command
[0] == 'p')
2498 ? FILTER_ACTION_PERMIT
2499 : FILTER_ACTION_DENY
;
2501 if (MATCH("ip", argv
[0]))
2502 rule
.proto
= IPPROTO_IP
;
2503 else if (MATCH("udp", argv
[0]))
2504 rule
.proto
= IPPROTO_UDP
;
2505 else if (MATCH("tcp", argv
[0]))
2506 rule
.proto
= IPPROTO_TCP
;
2509 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2513 for (a
= 1, i
= 0; i
< 2; i
++)
2517 ip_filter_portt
*port
;
2522 wild
= &rule
.src_wild
;
2523 port
= &rule
.src_ports
;
2528 wild
= &rule
.dst_wild
;
2529 port
= &rule
.dst_ports
;
2532 cli_print(cli
, "Specify destination");
2537 if (MATCH("any", argv
[a
]))
2540 *wild
= INADDR_BROADCAST
;
2543 else if (MATCH("host", argv
[a
]))
2547 cli_print(cli
, "Specify host ip address");
2551 if (!inet_aton(argv
[a
], &addr
))
2553 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2565 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2569 if (!inet_aton(argv
[a
], &addr
))
2571 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2577 if (!inet_aton(argv
[++a
], &addr
))
2579 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2583 *wild
= addr
.s_addr
;
2587 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2591 if (MATCH("eq", argv
[a
]))
2592 port
->op
= FILTER_PORT_OP_EQ
;
2593 else if (MATCH("neq", argv
[a
]))
2594 port
->op
= FILTER_PORT_OP_NEQ
;
2595 else if (MATCH("gt", argv
[a
]))
2596 port
->op
= FILTER_PORT_OP_GT
;
2597 else if (MATCH("lt", argv
[a
]))
2598 port
->op
= FILTER_PORT_OP_LT
;
2599 else if (MATCH("range", argv
[a
]))
2600 port
->op
= FILTER_PORT_OP_RANGE
;
2607 cli_print(cli
, "Specify port");
2611 if (!(port
->port
= atoi(argv
[a
])))
2613 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2618 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2623 cli_print(cli
, "Specify port");
2627 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2629 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2636 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2638 if (MATCH("established", argv
[a
]))
2640 rule
.tcp_flag_op
= FILTER_FLAG_OP_ANY
;
2641 rule
.tcp_sflags
= (TCP_FLAG_ACK
|TCP_FLAG_FIN
);
2642 rule
.tcp_cflags
= TCP_FLAG_SYN
;
2645 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2646 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2648 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2649 ? FILTER_FLAG_OP_ANY
2650 : FILTER_FLAG_OP_ALL
;
2654 cli_print(cli
, "Specify tcp flags");
2658 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2662 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2664 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2665 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2666 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2667 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2668 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2669 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2672 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2683 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2690 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2692 static ip_filter_rulet rule
;
2693 struct in_addr addr
;
2695 if (CLI_HELP_REQUESTED
)
2699 cli_arg_help(cli
, argv
[0][1],
2700 "A.B.C.D", "Source address",
2701 "any", "Any source address",
2702 "host", "Source host",
2708 if (MATCH("any", argv
[0]))
2710 if (argc
== 2 && !argv
[1][1])
2711 cli_arg_help(cli
, 1, NULL
);
2713 else if (MATCH("host", argv
[0]))
2717 cli_arg_help(cli
, argv
[1][1],
2718 "A.B.C.D", "Host address",
2721 else if (argc
== 3 && !argv
[2][1])
2722 cli_arg_help(cli
, 1, NULL
);
2728 cli_arg_help(cli
, 1,
2729 "A.B.C.D", "Wildcard bits",
2732 else if (argc
== 3 && !argv
[2][1])
2733 cli_arg_help(cli
, 1, NULL
);
2741 cli_print(cli
, "Specify rule details");
2745 memset(&rule
, 0, sizeof(rule
));
2746 rule
.action
= (command
[0] == 'p')
2747 ? FILTER_ACTION_PERMIT
2748 : FILTER_ACTION_DENY
;
2750 rule
.proto
= IPPROTO_IP
;
2751 if (MATCH("any", argv
[0]))
2753 rule
.src_ip
= INADDR_ANY
;
2754 rule
.src_wild
= INADDR_BROADCAST
;
2756 else if (MATCH("host", argv
[0]))
2760 cli_print(cli
, "Specify host ip address");
2764 if (!inet_aton(argv
[1], &addr
))
2766 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2770 rule
.src_ip
= addr
.s_addr
;
2771 rule
.src_wild
= INADDR_ANY
;
2777 cli_print(cli
, "Specify source ip address and wildcard");
2781 if (!inet_aton(argv
[0], &addr
))
2783 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2787 rule
.src_ip
= addr
.s_addr
;
2791 if (!inet_aton(argv
[1], &addr
))
2793 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2797 rule
.src_wild
= addr
.s_addr
;
2800 rule
.src_wild
= INADDR_ANY
;
2806 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2809 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2810 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2811 : access_list_rule_std(cli
, command
, argv
, argc
);
2816 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2818 if (!ip_filters
[filt
].rules
[i
].action
)
2820 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2824 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2828 cli_print(cli
, "Too many rules");
2832 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2837 /* filter USER {in|out} FILTER ... */
2838 if (CLI_HELP_REQUESTED
)
2843 return cli_arg_help(cli
, 0,
2844 "USER", "Username of session to filter", NULL
);
2848 return cli_arg_help(cli
, 0,
2849 "in", "Set incoming filter",
2850 "out", "Set outgoing filter", NULL
);
2854 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2855 "NAME", "Filter name", NULL
);
2858 return cli_arg_help(cli
, argc
> 1, NULL
);
2862 if (!config
->cluster_iam_master
)
2864 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
2868 if (argc
!= 3 && argc
!= 5)
2870 cli_print(cli
, "Specify a user and filters");
2874 if (!(s
= sessionbyuser(argv
[0])))
2876 cli_print(cli
, "User %s is not connected", argv
[0]);
2880 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2881 for (i
= 1; i
< argc
; i
+= 2)
2886 if (MATCH("in", argv
[i
]))
2888 if (session
[s
].filter_in
)
2890 cli_print(cli
, "Input already filtered");
2893 f
= &cli_session_actions
[s
].filter_in
;
2895 else if (MATCH("out", argv
[i
]))
2897 if (session
[s
].filter_out
)
2899 cli_print(cli
, "Output already filtered");
2902 f
= &cli_session_actions
[s
].filter_out
;
2906 cli_print(cli
, "Invalid filter specification");
2910 v
= find_access_list(argv
[i
+1]);
2911 if (v
< 0 || !*ip_filters
[v
].name
)
2913 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2920 cli_print(cli
, "Filtering user %s", argv
[0]);
2921 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2926 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2931 if (CLI_HELP_REQUESTED
)
2932 return cli_arg_help(cli
, argc
> 1,
2933 "USER", "Username of session to remove filters from", NULL
);
2935 if (!config
->cluster_iam_master
)
2937 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
2943 cli_print(cli
, "Specify a user to remove filters from");
2947 for (i
= 0; i
< argc
; i
++)
2949 if (!(s
= sessionbyuser(argv
[i
])))
2951 cli_print(cli
, "User %s is not connected", argv
[i
]);
2955 if (session
[s
].filter_in
|| session
[s
].filter_out
)
2957 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
2958 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
2962 cli_print(cli
, "User %s not filtered", argv
[i
]);
2969 // Convert a string in the form of abcd.ef12.3456 into char[6]
2970 void parsemac(char *string
, char mac
[6])
2972 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)
2974 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)