1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.37 2004-11-29 12:36:54 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
);
120 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
122 /* match if b is a substr of a */
123 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
125 void init_cli(char *hostname
)
129 struct cli_command
*c
;
130 struct cli_command
*c2
;
132 struct sockaddr_in addr
;
135 if (hostname
&& *hostname
)
136 cli_set_hostname(cli
, hostname
);
138 cli_set_hostname(cli
, "l2tpns");
140 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
141 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
143 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
145 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
146 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
147 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
148 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
149 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
150 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
151 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
152 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
153 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
154 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
155 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");
156 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
157 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
159 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
160 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
161 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
164 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
166 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
167 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
170 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
172 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
173 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
174 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
176 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
177 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
178 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
179 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
182 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
183 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
186 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
187 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
188 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
189 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
190 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
193 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
194 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
196 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
197 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
199 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
200 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
202 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
204 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
205 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
208 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
209 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
210 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
211 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
213 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
214 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
216 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
217 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
219 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
221 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
222 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
224 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
225 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
227 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
228 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
229 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
231 // Enable regular processing
232 cli_regular(cli
, regular_stuff
);
234 if (!(f
= fopen(CLIUSERS
, "r")))
236 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
240 while (fgets(buf
, 4096, f
))
243 if (*buf
== '#') continue;
244 if ((p
= strchr(buf
, '\r'))) *p
= 0;
245 if ((p
= strchr(buf
, '\n'))) *p
= 0;
247 if (!(p
= strchr((char *)buf
, ':'))) continue;
249 if (!strcmp(buf
, "enable"))
251 cli_allow_enable(cli
, p
);
252 LOG(3, 0, 0, "Setting enable password\n");
256 cli_allow_user(cli
, buf
, p
);
257 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
263 memset(&addr
, 0, sizeof(addr
));
264 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
265 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
268 // Set cli fd as non-blocking
269 flags
= fcntl(clifd
, F_GETFL
, 0);
270 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
272 addr
.sin_family
= AF_INET
;
273 addr
.sin_port
= htons(23);
274 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
276 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
282 void cli_do(int sockfd
)
284 int require_auth
= 1;
285 struct sockaddr_in addr
;
286 int l
= sizeof(addr
);
288 if (fork_and_close()) return;
289 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
291 LOG(3, 0, 0, "Accepted connection to CLI from %s\n", fmtaddr(addr
.sin_addr
.s_addr
, 0));
292 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
295 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
299 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
300 if (!cli
->users
) /* paranoia */
302 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
308 /* no username/pass required */
315 debug_rb_tail
= ringbuffer
->tail
;
317 memset(&debug_flags
, 0, sizeof(debug_flags
));
318 debug_flags
.critical
= 1;
320 cli_loop(cli
, sockfd
);
323 LOG(3, 0, 0, "Closed CLI connection from %s\n", fmtaddr(addr
.sin_addr
.s_addr
, 0));
327 static void cli_print_log(struct cli_def
*cli
, char *string
)
329 LOG(3, 0, 0, "%s\n", string
);
332 void cli_do_file(FILE *fh
)
334 LOG(3, 0, 0, "Reading configuration file\n");
335 cli_print_callback(cli
, cli_print_log
);
336 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
337 cli_print_callback(cli
, NULL
);
340 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
351 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
353 int v
= va_arg(ap
, int);
354 snprintf(buf
, sizeof(buf
), entry
, v
);
360 desc
= va_arg(ap
, char *);
362 cli_print(cli
, " %-20s %s", p
, desc
);
364 cli_print(cli
, " %s", p
);
366 entry
= desc
? va_arg(ap
, char *) : 0;
371 cli_print(cli
, " <cr>");
376 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
380 if (CLI_HELP_REQUESTED
)
381 return cli_arg_help(cli
, 1,
382 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
388 // Show individual session
389 for (i
= 0; i
< argc
; i
++)
391 unsigned int s
, b_in
, b_out
;
393 if (s
<= 0 || s
>= MAXSESSION
)
395 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
398 cli_print(cli
, "\r\nSession %d:", s
);
399 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
400 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
401 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
402 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
403 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
404 cli_print(cli
, "\tUnique SID:\t%lu", session
[s
].unique_id
);
405 cli_print(cli
, "\tIdle time:\t%u seconds", abs(time_now
- session
[s
].last_packet
));
406 cli_print(cli
, "\tNext Recv:\t%u", session
[s
].nr
);
407 cli_print(cli
, "\tNext Send:\t%u", session
[s
].ns
);
408 cli_print(cli
, "\tBytes In/Out:\t%lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
409 cli_print(cli
, "\tPkts In/Out:\t%lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
410 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
411 cli_print(cli
, "\tRadius Session:\t%u", session
[s
].radius
);
412 cli_print(cli
, "\tRx Speed:\t%lu", session
[s
].rx_connect_speed
);
413 cli_print(cli
, "\tTx Speed:\t%lu", session
[s
].tx_connect_speed
);
414 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
415 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
416 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
417 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
418 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
419 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
421 cli_print(cli
, "\tIntercepted:\tno");
423 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
425 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
426 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
427 t
? "YES" : "no", t
? " (" : "",
428 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
430 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
434 b_in
= session
[s
].tbf_in
;
435 b_out
= session
[s
].tbf_out
;
437 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
438 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
439 "ByteSent", "PackSent", "PackDrop", "PackDelay");
442 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
444 (filter_list
[b_in
].next
? "*" : " "),
445 (b_in
< 100 ? "\t" : ""),
446 filter_list
[b_in
].rate
* 8,
447 filter_list
[b_in
].credit
,
448 filter_list
[b_in
].queued
,
449 filter_list
[b_in
].b_queued
,
450 filter_list
[b_in
].p_queued
,
451 filter_list
[b_in
].b_sent
,
452 filter_list
[b_in
].p_sent
,
453 filter_list
[b_in
].p_dropped
,
454 filter_list
[b_in
].p_delayed
);
457 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
459 (filter_list
[b_out
].next
? "*" : " "),
460 (b_out
< 100 ? "\t" : ""),
461 filter_list
[b_out
].rate
* 8,
462 filter_list
[b_out
].credit
,
463 filter_list
[b_out
].queued
,
464 filter_list
[b_out
].b_queued
,
465 filter_list
[b_out
].p_queued
,
466 filter_list
[b_out
].b_sent
,
467 filter_list
[b_out
].p_sent
,
468 filter_list
[b_out
].p_dropped
,
469 filter_list
[b_out
].p_delayed
);
476 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
491 for (i
= 1; i
< MAXSESSION
; i
++)
493 if (!session
[i
].opened
) continue;
494 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
497 session
[i
].user
[0] ? session
[i
].user
: "*",
498 fmtaddr(htonl(session
[i
].ip
), 0),
499 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
500 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
501 (session
[i
].walled_garden
) ? "Y" : "N",
502 abs(time_now
- (unsigned long)session
[i
].opened
),
503 (unsigned long)session
[i
].total_cout
,
504 (unsigned long)session
[i
].total_cin
,
505 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
506 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
507 session
[i
].calling
[0] ? session
[i
].calling
: "*");
512 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
514 int i
, x
, show_all
= 0;
522 if (CLI_HELP_REQUESTED
)
525 return cli_arg_help(cli
, 1,
526 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
529 return cli_arg_help(cli
, 1,
530 "all", "Show all tunnels, including unused",
531 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
538 if (strcmp(argv
[0], "all") == 0)
544 // Show individual tunnel
545 for (i
= 0; i
< argc
; i
++)
550 if (t
<= 0 || t
>= MAXTUNNEL
)
552 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
555 cli_print(cli
, "\r\nTunnel %d:", t
);
556 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
557 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
558 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
559 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
560 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
561 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
562 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
563 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
564 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
566 for (x
= 0; x
< MAXSESSION
; x
++)
567 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
568 sprintf(s
, "%s%u ", s
, x
);
570 cli_print(cli
, "\tSessions:\t%s", s
);
576 // Show tunnel summary
577 cli_print(cli
, "%4s %20s %20s %6s %s",
584 for (i
= 1; i
< MAXTUNNEL
; i
++)
587 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
589 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
590 cli_print(cli
, "%4d %20s %20s %6s %6d",
592 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
593 fmtaddr(htonl(tunnel
[i
].ip
), 0),
594 states
[tunnel
[i
].state
],
601 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
608 if (CLI_HELP_REQUESTED
)
609 return cli_arg_help(cli
, 1,
610 "USER", "Show details for specific username",
613 for (i
= 0; i
< MAXSESSION
; i
++)
615 if (!session
[i
].opened
) continue;
616 if (!session
[i
].user
[0]) continue;
620 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
622 if (strcmp(argv
[j
], session
[i
].user
) == 0)
624 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
625 sargv
[sargc
] = sid
[sargc
];
633 cli_print(cli
, "%s", session
[i
].user
);
637 return cmd_show_session(cli
, "users", sargv
, sargc
);
642 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
644 if (CLI_HELP_REQUESTED
)
645 return CLI_HELP_NO_ARGS
;
647 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
648 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
649 GET_STAT(tun_rx_bytes
),
650 GET_STAT(tun_rx_packets
),
651 GET_STAT(tun_rx_errors
));
652 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
653 GET_STAT(tun_tx_bytes
),
654 GET_STAT(tun_tx_packets
),
655 GET_STAT(tun_tx_errors
));
658 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
659 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
660 GET_STAT(tunnel_rx_bytes
),
661 GET_STAT(tunnel_rx_packets
),
662 GET_STAT(tunnel_rx_errors
),
664 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
665 GET_STAT(tunnel_tx_bytes
),
666 GET_STAT(tunnel_tx_packets
),
667 GET_STAT(tunnel_tx_errors
),
668 GET_STAT(tunnel_retries
));
671 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
672 cli_print(cli
, "-----------------------------------------");
673 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
674 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
675 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
676 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
677 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
678 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
679 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
680 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
681 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
682 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
683 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
684 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
685 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
686 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
687 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
691 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
692 cli_print(cli
, "-----------------------------------------");
693 cli_print(cli
, "%-30s%lu", "call_processtun", GET_STAT(call_processtun
));
694 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
695 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
696 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
697 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
698 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
699 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
700 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
701 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
702 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
703 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
704 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
705 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
706 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
707 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
708 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
709 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
710 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
711 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
712 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
713 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
714 cli_print(cli
, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
715 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
716 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
717 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
718 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
723 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
729 if (CLI_HELP_REQUESTED
)
730 return cli_arg_help(cli
, 1,
731 "tag", "Include CVS release tag",
732 "file", "Include file versions",
735 for (i
= 0; i
< argc
; i
++)
736 if (!strcmp(argv
[i
], "tag"))
738 else if (!strcmp(argv
[i
], "file"))
741 cli_print(cli
, "L2TPNS %s", VERSION
);
744 char const *p
= strchr(cvs_name
, ':');
756 e
= strpbrk(p
, " \t$");
757 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
762 extern linked_list
*loaded_plugins
;
765 cli_print(cli
, "Files:");
766 cli_print(cli
, " %s", cvs_id_arp
);
768 cli_print(cli
, " %s", cvs_id_bgp
);
770 cli_print(cli
, " %s", cvs_id_cli
);
771 cli_print(cli
, " %s", cvs_id_cluster
);
772 cli_print(cli
, " %s", cvs_id_constants
);
773 cli_print(cli
, " %s", cvs_id_control
);
774 cli_print(cli
, " %s", cvs_id_icmp
);
775 cli_print(cli
, " %s", cvs_id_l2tpns
);
776 cli_print(cli
, " %s", cvs_id_ll
);
777 cli_print(cli
, " %s", cvs_id_md5
);
778 cli_print(cli
, " %s", cvs_id_ppp
);
779 cli_print(cli
, " %s", cvs_id_radius
);
780 cli_print(cli
, " %s", cvs_id_tbf
);
781 cli_print(cli
, " %s", cvs_id_util
);
783 ll_reset(loaded_plugins
);
784 while ((p
= ll_next(loaded_plugins
)))
786 char const **id
= dlsym(p
, "cvs_id");
788 cli_print(cli
, " %s", *id
);
795 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
798 int used
= 0, free
= 0, show_all
= 0;
800 if (!config
->cluster_iam_master
)
802 cli_print(cli
, "Can't do this on a slave. Do it on %s",
803 fmtaddr(config
->cluster_master_address
, 0));
808 if (CLI_HELP_REQUESTED
)
811 return cli_arg_help(cli
, 1, NULL
);
813 return cli_arg_help(cli
, 1,
814 "all", "Show all pool addresses, including unused",
818 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
822 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
823 for (i
= 0; i
< MAXIPPOOL
; i
++)
825 if (!ip_address_pool
[i
].address
) continue;
826 if (ip_address_pool
[i
].assigned
)
828 cli_print(cli
, "%-15s\tY %8d %s",
829 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
830 ip_address_pool
[i
].session
,
831 session
[ip_address_pool
[i
].session
].user
);
837 if (ip_address_pool
[i
].last
)
838 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
839 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
840 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
843 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
850 cli_print(cli
, "(Not displaying unused addresses)");
852 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
856 static FILE *save_config_fh
= 0;
857 static void print_save_config(struct cli_def
*cli
, char *string
)
860 fprintf(save_config_fh
, "%s\n", string
);
863 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
865 if (CLI_HELP_REQUESTED
)
866 return CLI_HELP_NO_ARGS
;
868 if ((save_config_fh
= fopen(config
->config_file
, "w")))
870 cli_print(cli
, "Writing configuration");
871 cli_print_callback(cli
, print_save_config
);
872 cmd_show_run(cli
, command
, argv
, argc
);
873 cli_print_callback(cli
, NULL
);
874 fclose(save_config_fh
);
879 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
884 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
886 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
890 if (CLI_HELP_REQUESTED
)
891 return CLI_HELP_NO_ARGS
;
893 cli_print(cli
, "# Current configuration:");
895 for (i
= 0; config_values
[i
].key
; i
++)
897 void *value
= ((void *)config
) + config_values
[i
].offset
;
898 if (config_values
[i
].type
== STRING
)
899 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
900 else if (config_values
[i
].type
== IP
)
901 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(unsigned *)value
, 0));
902 else if (config_values
[i
].type
== SHORT
)
903 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
904 else if (config_values
[i
].type
== BOOL
)
905 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
906 else if (config_values
[i
].type
== INT
)
907 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
908 else if (config_values
[i
].type
== UNSIGNED_LONG
)
909 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
910 else if (config_values
[i
].type
== MAC
)
911 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
912 *(unsigned short *)(value
+ 0),
913 *(unsigned short *)(value
+ 1),
914 *(unsigned short *)(value
+ 2),
915 *(unsigned short *)(value
+ 3),
916 *(unsigned short *)(value
+ 4),
917 *(unsigned short *)(value
+ 5));
920 cli_print(cli
, "# Plugins");
921 for (i
= 0; i
< MAXPLUGINS
; i
++)
923 if (*config
->plugins
[i
])
925 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
930 if (config
->as_number
)
935 cli_print(cli
, "# BGP");
936 cli_print(cli
, "router bgp %u", config
->as_number
);
937 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
939 if (!config
->neighbour
[i
].name
[0])
942 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
944 k
= config
->neighbour
[i
].keepalive
;
945 h
= config
->neighbour
[i
].hold
;
952 k
= BGP_KEEPALIVE_TIME
;
958 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
963 cli_print(cli
, "# Filters");
964 for (i
= 0; i
< MAXFILTER
; i
++)
966 ip_filter_rulet
*rules
;
967 if (!*ip_filters
[i
].name
)
970 cli_print(cli
, "ip access-list %s %s",
971 ip_filters
[i
].extended
? "extended" : "standard",
974 rules
= ip_filters
[i
].rules
;
975 while (rules
->action
)
976 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
979 cli_print(cli
, "# end");
983 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
985 int i
, free
= 0, used
= 0, show_all
= 0;
996 if (CLI_HELP_REQUESTED
)
999 return cli_arg_help(cli
, 1, NULL
);
1001 return cli_arg_help(cli
, 1,
1002 "all", "Show all RADIUS sessions, including unused",
1006 cli_print(cli
, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1010 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1013 for (i
= 1; i
< MAXRADIUS
; i
++)
1015 if (radius
[i
].state
== RADIUSNULL
)
1020 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1022 cli_print(cli
, "%6d%6d%5d%6s%9d%9u%4d",
1026 states
[radius
[i
].state
],
1032 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1037 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1041 if (CLI_HELP_REQUESTED
)
1042 return CLI_HELP_NO_ARGS
;
1044 cli_print(cli
, "Plugins currently loaded:");
1045 for (i
= 0; i
< MAXPLUGINS
; i
++)
1046 if (*config
->plugins
[i
])
1047 cli_print(cli
, " %s", config
->plugins
[i
]);
1052 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1056 if (CLI_HELP_REQUESTED
)
1057 return CLI_HELP_NO_ARGS
;
1059 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1068 for (i
= 0; i
< MAXSESSION
; i
++)
1070 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1071 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1075 session
[i
].throttle_in
,
1076 session
[i
].throttle_out
,
1078 session
[i
].tbf_out
);
1084 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1086 if (CLI_HELP_REQUESTED
)
1087 return CLI_HELP_NO_ARGS
;
1089 cli_print(cli
, " _\n"
1095 " ( \\ `. `-. _,.-:\\\n"
1096 " \\ \\ `. `-._ __..--' ,-';/\n"
1097 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1098 " `. `. `-._ __..--' ,' /\n"
1099 " `. `-_ ``--..'' _.-' ,'\n"
1100 " `-_ `-.___ __,--' ,'\n"
1101 " `-.__ `----\"\"\" __.-'\n"
1102 "hh `--..____..--'");
1107 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1109 if (CLI_HELP_REQUESTED
)
1110 return CLI_HELP_NO_ARGS
;
1112 cli_print(cli
, "Counters cleared");
1113 SET_STAT(last_reset
, time(NULL
));
1117 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1122 if (CLI_HELP_REQUESTED
)
1123 return cli_arg_help(cli
, argc
> 1,
1124 "USER", "Username of session to drop", NULL
);
1126 if (!config
->cluster_iam_master
)
1128 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1129 fmtaddr(config
->cluster_master_address
, 0));
1136 cli_print(cli
, "Specify a user to drop");
1140 for (i
= 0; i
< argc
; i
++)
1142 if (!(s
= sessionbyuser(argv
[i
])))
1144 cli_print(cli
, "User %s is not connected", argv
[i
]);
1148 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1150 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1151 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1158 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1163 if (CLI_HELP_REQUESTED
)
1164 return cli_arg_help(cli
, argc
> 1,
1165 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1167 if (!config
->cluster_iam_master
)
1169 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1170 fmtaddr(config
->cluster_master_address
, 0));
1177 cli_print(cli
, "Specify a tunnel to drop");
1181 for (i
= 0; i
< argc
; i
++)
1183 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1185 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1191 cli_print(cli
, "Tunnel %d is not connected", t
);
1197 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1201 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1202 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1208 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1213 if (CLI_HELP_REQUESTED
)
1214 return cli_arg_help(cli
, argc
> 1,
1215 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1217 if (!config
->cluster_iam_master
)
1219 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1220 fmtaddr(config
->cluster_master_address
, 0));
1227 cli_print(cli
, "Specify a session id to drop");
1231 for (i
= 0; i
< argc
; i
++)
1233 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1235 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1239 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1241 cli_print(cli
, "Dropping session %d", s
);
1242 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1246 cli_print(cli
, "Session %d is not active.", s
);
1253 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1259 if (CLI_HELP_REQUESTED
)
1264 return cli_arg_help(cli
, 0,
1265 "USER", "Username of session to snoop", NULL
);
1268 return cli_arg_help(cli
, 0,
1269 "A.B.C.D", "IP address of snoop destination", NULL
);
1272 return cli_arg_help(cli
, 0,
1273 "N", "Port of snoop destination", NULL
);
1277 return cli_arg_help(cli
, 1, NULL
);
1284 if (!config
->cluster_iam_master
)
1286 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1287 fmtaddr(config
->cluster_master_address
, 0));
1294 cli_print(cli
, "Specify username, ip and port");
1298 if (!(s
= sessionbyuser(argv
[0])))
1300 cli_print(cli
, "User %s is not connected", argv
[0]);
1304 ip
= inet_addr(argv
[1]);
1305 if (!ip
|| ip
== INADDR_NONE
)
1307 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1311 port
= atoi(argv
[2]);
1314 cli_print(cli
, "Invalid port %s", argv
[2]);
1318 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1319 cli_session_actions
[s
].snoop_ip
= ip
;
1320 cli_session_actions
[s
].snoop_port
= port
;
1321 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1326 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1331 if (CLI_HELP_REQUESTED
)
1332 return cli_arg_help(cli
, argc
> 1,
1333 "USER", "Username of session to unsnoop", NULL
);
1335 if (!config
->cluster_iam_master
)
1337 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1338 fmtaddr(config
->cluster_master_address
, 0));
1345 cli_print(cli
, "Specify a user to unsnoop");
1349 for (i
= 0; i
< argc
; i
++)
1351 if (!(s
= sessionbyuser(argv
[i
])))
1353 cli_print(cli
, "User %s is not connected", argv
[i
]);
1357 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1358 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1364 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1371 throttle USER - throttle in/out to default rate
1372 throttle USER RATE - throttle in/out to default rate
1373 throttle USER in RATE - throttle input only
1374 throttle USER out RATE - throttle output only
1375 throttle USER in RATE out RATE - throttle both
1378 if (CLI_HELP_REQUESTED
)
1383 return cli_arg_help(cli
, 0,
1384 "USER", "Username of session to throttle", NULL
);
1387 return cli_arg_help(cli
, 1,
1388 "RATE", "Rate in kbps (in and out)",
1389 "in", "Select incoming rate",
1390 "out", "Select outgoing rate", NULL
);
1393 return cli_arg_help(cli
, 1,
1394 "in", "Select incoming rate",
1395 "out", "Select outgoing rate", NULL
);
1398 if (isdigit(argv
[1][0]))
1399 return cli_arg_help(cli
, 1, NULL
);
1402 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1405 return cli_arg_help(cli
, argc
> 1, NULL
);
1409 if (!config
->cluster_iam_master
)
1411 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1412 fmtaddr(config
->cluster_master_address
, 0));
1419 cli_print(cli
, "Specify a user to throttle");
1423 if (!(s
= sessionbyuser(argv
[0])))
1425 cli_print(cli
, "User %s is not connected", argv
[0]);
1431 rate_in
= rate_out
= config
->rl_rate
;
1435 rate_in
= rate_out
= atoi(argv
[1]);
1438 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1442 else if (argc
== 3 || argc
== 5)
1445 for (i
= 1; i
< argc
- 1; i
+= 2)
1448 if (MATCH("in", argv
[i
]))
1449 r
= rate_in
= atoi(argv
[i
+1]);
1450 else if (MATCH("out", argv
[i
]))
1451 r
= rate_out
= atoi(argv
[i
+1]);
1455 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1462 cli_print(cli
, "Invalid arguments");
1466 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1468 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1472 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1473 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1474 cli_session_actions
[s
].throttle_in
= rate_in
;
1476 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1477 cli_session_actions
[s
].throttle_out
= rate_out
;
1479 if (cli_session_actions
[s
].throttle_in
== -1 &&
1480 cli_session_actions
[s
].throttle_out
== -1)
1482 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1486 cli_print(cli
, "Throttling user %s", argv
[0]);
1487 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1492 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1497 if (CLI_HELP_REQUESTED
)
1498 return cli_arg_help(cli
, argc
> 1,
1499 "USER", "Username of session to unthrottle", NULL
);
1501 if (!config
->cluster_iam_master
)
1503 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1504 fmtaddr(config
->cluster_master_address
, 0));
1511 cli_print(cli
, "Specify a user to unthrottle");
1515 for (i
= 0; i
< argc
; i
++)
1517 if (!(s
= sessionbyuser(argv
[i
])))
1519 cli_print(cli
, "User %s is not connected", argv
[i
]);
1523 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1525 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1526 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1530 cli_print(cli
, "User %s not throttled", argv
[i
]);
1537 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1541 if (CLI_HELP_REQUESTED
)
1542 return cli_arg_help(cli
, 1,
1543 "all", "Enable debugging for all except \"data\"",
1544 "critical", "", // FIXME: add descriptions
1554 char *p
= (char *) &debug_flags
;
1555 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1559 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1560 (debug_flags
.critical
) ? " critical" : "",
1561 (debug_flags
.error
) ? " error" : "",
1562 (debug_flags
.warning
) ? " warning" : "",
1563 (debug_flags
.info
) ? " info" : "",
1564 (debug_flags
.calls
) ? " calls" : "",
1565 (debug_flags
.data
) ? " data" : "");
1571 cli_print(cli
, "Debugging off");
1575 for (i
= 0; i
< argc
; i
++)
1577 int len
= strlen(argv
[i
]);
1579 if (argv
[i
][0] == 'c' && len
< 2)
1580 len
= 2; /* distinguish [cr]itical from [ca]lls */
1582 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1583 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1584 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1585 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1586 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1587 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1588 if (!strncmp("all", argv
[i
], len
))
1590 memset(&debug_flags
, 1, sizeof(debug_flags
));
1591 debug_flags
.data
= 0;
1595 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1601 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1605 if (CLI_HELP_REQUESTED
)
1606 return cli_arg_help(cli
, 1,
1607 "all", "Disable all debugging",
1608 "critical", "", // FIXME: add descriptions
1618 memset(&debug_flags
, 0, sizeof(debug_flags
));
1622 for (i
= 0; i
< argc
; i
++)
1624 int len
= strlen(argv
[i
]);
1626 if (argv
[i
][0] == 'c' && len
< 2)
1627 len
= 2; /* distinguish [cr]itical from [ca]lls */
1629 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1630 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1631 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1632 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1633 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1634 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1635 if (!strncmp("all", argv
[i
], len
))
1637 memset(&debug_flags
, 0, sizeof(debug_flags
));
1641 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1647 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1649 int i
, firstfree
= 0;
1651 if (CLI_HELP_REQUESTED
)
1652 return cli_arg_help(cli
, argc
> 1,
1653 "PLUGIN", "Name of plugin to load", NULL
);
1657 cli_print(cli
, "Specify a plugin to load");
1661 for (i
= 0; i
< MAXPLUGINS
; i
++)
1663 if (!*config
->plugins
[i
] && !firstfree
)
1665 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1667 cli_print(cli
, "Plugin is already loaded");
1674 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1675 config
->reload_config
= 1;
1676 cli_print(cli
, "Loading plugin %s", argv
[0]);
1682 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1686 if (CLI_HELP_REQUESTED
)
1687 return cli_arg_help(cli
, argc
> 1,
1688 "PLUGIN", "Name of plugin to unload", NULL
);
1692 cli_print(cli
, "Specify a plugin to remove");
1696 for (i
= 0; i
< MAXPLUGINS
; i
++)
1698 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1700 config
->reload_config
= 1;
1701 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1706 cli_print(cli
, "Plugin is not loaded");
1710 static char *duration(time_t secs
)
1712 static char *buf
= NULL
;
1715 if (!buf
) buf
= calloc(64, 1);
1719 int days
= secs
/ 86400;
1720 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1726 int mins
= secs
/ 60;
1727 int hrs
= mins
/ 60;
1730 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1732 else if (secs
>= 60)
1734 int mins
= secs
/ 60;
1735 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1738 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1743 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1746 char buf
[100], *p
= buf
, *loads
[3];
1747 int i
, num_sessions
= 0;
1749 if (CLI_HELP_REQUESTED
)
1750 return CLI_HELP_NO_ARGS
;
1752 fh
= fopen("/proc/loadavg", "r");
1753 fgets(buf
, 100, fh
);
1756 for (i
= 0; i
< 3; i
++)
1757 loads
[i
] = strdup(strsep(&p
, " "));
1760 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1762 for (i
= 1; i
< MAXSESSION
; i
++)
1763 if (session
[i
].opened
) num_sessions
++;
1765 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1767 duration(time_now
- config
->start_time
),
1769 loads
[0], loads
[1], loads
[2]
1771 for (i
= 0; i
< 3; i
++)
1772 if (loads
[i
]) free(loads
[i
]);
1774 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1779 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1783 if (CLI_HELP_REQUESTED
)
1789 int len
= strlen(argv
[0])-1;
1790 for (i
= 0; config_values
[i
].key
; i
++)
1791 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1792 cli_print(cli
, " %s", config_values
[i
].key
);
1798 return cli_arg_help(cli
, 0,
1799 "VALUE", "Value for variable", NULL
);
1803 return cli_arg_help(cli
, 1, NULL
);
1812 cli_print(cli
, "Specify variable and value");
1816 for (i
= 0; config_values
[i
].key
; i
++)
1818 void *value
= ((void *)config
) + config_values
[i
].offset
;
1819 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1821 // Found a value to set
1822 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1823 switch (config_values
[i
].type
)
1826 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1829 *(int *)value
= atoi(argv
[1]);
1832 *(unsigned long *)value
= atol(argv
[1]);
1835 *(short *)value
= atoi(argv
[1]);
1838 *(unsigned *)value
= inet_addr(argv
[1]);
1841 parsemac(argv
[1], (char *)value
);
1844 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1850 cli_print(cli
, "Unknown variable type");
1853 config
->reload_config
= 1;
1858 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1862 int regular_stuff(struct cli_def
*cli
)
1864 int i
= debug_rb_tail
;
1868 while (i
!= ringbuffer
->tail
)
1870 int show_message
= 0;
1872 if (*ringbuffer
->buffer
[i
].message
)
1874 // Always show messages if we are doing general debug
1875 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1876 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1877 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1878 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1879 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1880 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1885 cli_print(cli
, "\r%s-%u-%u %s",
1886 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1887 ringbuffer
->buffer
[i
].tunnel
,
1888 ringbuffer
->buffer
[i
].session
,
1889 ringbuffer
->buffer
[i
].message
);
1894 if (++i
== ringbuffer
->tail
) break;
1895 if (i
== RINGBUFFER_SIZE
) i
= 0;
1898 debug_rb_tail
= ringbuffer
->tail
;
1906 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1910 if (CLI_HELP_REQUESTED
)
1911 return cli_arg_help(cli
, argc
> 1,
1912 "<1-65535>", "Autonomous system number", NULL
);
1914 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1916 cli_print(cli
, "Invalid autonomous system number");
1920 if (bgp_configured
&& as
!= config
->as_number
)
1922 cli_print(cli
, "Can't change local AS on a running system");
1926 config
->as_number
= as
;
1927 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1932 static int find_bgp_neighbour(char const *name
)
1937 in_addr_t addrs
[4] = { 0 };
1940 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1943 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1944 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1946 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1948 if (!config
->neighbour
[i
].name
[0])
1950 if (new == -1) new = i
;
1954 if (!strcmp(name
, config
->neighbour
[i
].name
))
1957 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1960 for (a
= h
->h_addr_list
; *a
; a
++)
1963 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1964 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1972 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1978 if (CLI_HELP_REQUESTED
)
1983 return cli_arg_help(cli
, 0,
1984 "A.B.C.D", "BGP neighbour address",
1985 "NAME", "BGP neighbour name",
1989 return cli_arg_help(cli
, 0,
1990 "remote-as", "Set remote autonomous system number",
1991 "timers", "Set timers",
1995 if (MATCH("remote-as", argv
[1]))
1996 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
1998 if (MATCH("timers", argv
[1]))
2001 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2004 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2006 if (argc
== 5 && !argv
[4][1])
2007 return cli_arg_help(cli
, 1, NULL
);
2016 cli_print(cli
, "Invalid arguments");
2020 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2022 cli_print(cli
, "Invalid neighbour");
2028 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2032 if (MATCH("remote-as", argv
[1]))
2034 int as
= atoi(argv
[2]);
2035 if (as
< 0 || as
> 65535)
2037 cli_print(cli
, "Invalid autonomous system number");
2041 if (!config
->neighbour
[i
].name
[0])
2043 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2044 config
->neighbour
[i
].keepalive
= -1;
2045 config
->neighbour
[i
].hold
= -1;
2048 config
->neighbour
[i
].as
= as
;
2052 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2054 cli_print(cli
, "Invalid arguments");
2058 if (!config
->neighbour
[i
].name
[0])
2060 cli_print(cli
, "Specify remote-as first");
2064 keepalive
= atoi(argv
[2]);
2065 hold
= atoi(argv
[3]);
2067 if (keepalive
< 1 || keepalive
> 65535)
2069 cli_print(cli
, "Invalid keepalive time");
2073 if (hold
< 3 || hold
> 65535)
2075 cli_print(cli
, "Invalid hold time");
2079 if (keepalive
== BGP_KEEPALIVE_TIME
)
2080 keepalive
= -1; // using default value
2082 if (hold
== BGP_HOLD_TIME
)
2085 config
->neighbour
[i
].keepalive
= keepalive
;
2086 config
->neighbour
[i
].hold
= hold
;
2091 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2095 if (CLI_HELP_REQUESTED
)
2096 return cli_arg_help(cli
, argc
> 0,
2097 "A.B.C.D", "BGP neighbour address",
2098 "NAME", "BGP neighbour name",
2103 cli_print(cli
, "Specify a BGP neighbour");
2107 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2109 cli_print(cli
, "Invalid neighbour");
2113 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2115 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2119 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2123 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2129 if (!bgp_configured
)
2132 if (CLI_HELP_REQUESTED
)
2133 return cli_arg_help(cli
, 1,
2134 "A.B.C.D", "BGP neighbour address",
2135 "NAME", "BGP neighbour name",
2138 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2139 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2143 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2145 if (!*bgp_peers
[i
].name
)
2148 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2149 if (argc
&& strcmp(addr
, argv
[0]) &&
2150 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2156 cli_print(cli
, "Peer AS Address "
2157 "State Retries Retry in Route Pend Timers");
2158 cli_print(cli
, "------------------ ----- --------------- "
2159 "----------- ------- -------- ----- ---- ---------");
2162 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s %4d %4d",
2166 bgp_state_str(bgp_peers
[i
].state
),
2167 bgp_peers
[i
].retry_count
,
2168 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2169 bgp_peers
[i
].routing
? "yes" : "no",
2170 bgp_peers
[i
].update_routes
? "yes" : "no",
2171 bgp_peers
[i
].keepalive
,
2178 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2183 if (!bgp_configured
)
2186 if (CLI_HELP_REQUESTED
)
2187 return cli_arg_help(cli
, 1,
2188 "A.B.C.D", "BGP neighbour address",
2189 "NAME", "BGP neighbour name",
2192 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2194 if (bgp_peers
[i
].state
!= Established
)
2197 if (!bgp_peers
[i
].routing
)
2200 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2201 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2204 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2205 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2211 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2216 if (!bgp_configured
)
2219 if (CLI_HELP_REQUESTED
)
2220 return cli_arg_help(cli
, 1,
2221 "A.B.C.D", "BGP neighbour address",
2222 "NAME", "BGP neighbour name",
2225 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2227 if (bgp_peers
[i
].state
!= Established
)
2230 if (bgp_peers
[i
].routing
)
2233 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2234 if (argc
&& strcmp(addr
, argv
[0]) &&
2235 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2238 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2239 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2245 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2250 if (!bgp_configured
)
2253 if (CLI_HELP_REQUESTED
)
2254 return cli_arg_help(cli
, 1,
2255 "A.B.C.D", "BGP neighbour address",
2256 "NAME", "BGP neighbour name",
2259 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2261 if (!*bgp_peers
[i
].name
)
2264 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2265 if (argc
&& strcmp(addr
, argv
[0]) &&
2266 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2269 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2270 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2278 static int find_access_list(char const *name
)
2282 for (i
= 0; i
< MAXFILTER
; i
++)
2283 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2289 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2293 if (CLI_HELP_REQUESTED
)
2298 return cli_arg_help(cli
, 0,
2299 "standard", "Standard syntax",
2300 "extended", "Extended syntax",
2304 return cli_arg_help(cli
, argv
[1][1],
2305 "NAME", "Access-list name",
2309 if (argc
== 3 && !argv
[2][1])
2310 return cli_arg_help(cli
, 1, NULL
);
2318 cli_print(cli
, "Specify access-list type and name");
2322 if (MATCH("standard", argv
[0]))
2324 else if (MATCH("extended", argv
[0]))
2328 cli_print(cli
, "Invalid access-list type");
2332 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2333 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2335 cli_print(cli
, "Invalid access-list name");
2339 filt
= find_access_list(argv
[1]);
2344 cli_print(cli
, "Too many access-lists");
2349 if (!*ip_filters
[filt
].name
)
2351 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2352 strcpy(ip_filters
[filt
].name
, argv
[1]);
2353 ip_filters
[filt
].extended
= extended
;
2355 else if (ip_filters
[filt
].extended
!= extended
)
2357 cli_print(cli
, "Access-list is %s",
2358 ip_filters
[filt
].extended
? "extended" : "standard");
2363 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2367 if (filt
< 0 || !*ip_filters
[filt
].name
)
2369 cli_print(cli
, "Access-list not defined");
2374 if (ip_filters
[filt
].used
)
2376 cli_print(cli
, "Access-list in use");
2380 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2384 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2386 return access_list(cli
, argv
, argc
, 1);
2389 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2391 return access_list(cli
, argv
, argc
, 0);
2394 static int show_ip_wild(char *buf
, ipt ip
, ipt wild
)
2396 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2397 return sprintf(buf
, " any");
2399 if (wild
== INADDR_ANY
)
2400 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2402 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2405 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2409 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2410 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2411 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2412 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2413 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2419 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2421 static char buf
[256];
2424 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2427 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2428 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2431 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2435 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2436 p
+= show_ports(p
, &rule
->src_ports
);
2438 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2439 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2440 p
+= show_ports(p
, &rule
->dst_ports
);
2442 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2444 switch (rule
->tcp_flag_op
)
2446 case FILTER_FLAG_OP_EST
:
2447 p
+= sprintf(p
, " established");
2450 case FILTER_FLAG_OP_ANY
:
2451 case FILTER_FLAG_OP_ALL
:
2452 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2453 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2454 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2455 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2456 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2457 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2458 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2459 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2460 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2461 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2462 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2463 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2464 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2470 p
+= sprintf(p
, " fragments");
2475 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2477 static ip_filter_rulet rule
;
2478 struct in_addr addr
;
2482 if (CLI_HELP_REQUESTED
)
2486 cli_arg_help(cli
, 0,
2487 "ip", "Match IP packets",
2488 "tcp", "Match TCP packets",
2489 "udp", "Match UDP packets",
2495 // *sigh*, too darned complex
2496 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2502 cli_print(cli
, "Specify rule details");
2506 memset(&rule
, 0, sizeof(rule
));
2507 rule
.action
= (command
[0] == 'p')
2508 ? FILTER_ACTION_PERMIT
2509 : FILTER_ACTION_DENY
;
2511 if (MATCH("ip", argv
[0]))
2512 rule
.proto
= IPPROTO_IP
;
2513 else if (MATCH("udp", argv
[0]))
2514 rule
.proto
= IPPROTO_UDP
;
2515 else if (MATCH("tcp", argv
[0]))
2516 rule
.proto
= IPPROTO_TCP
;
2519 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2523 for (a
= 1, i
= 0; i
< 2; i
++)
2527 ip_filter_portt
*port
;
2532 wild
= &rule
.src_wild
;
2533 port
= &rule
.src_ports
;
2538 wild
= &rule
.dst_wild
;
2539 port
= &rule
.dst_ports
;
2542 cli_print(cli
, "Specify destination");
2547 if (MATCH("any", argv
[a
]))
2550 *wild
= INADDR_BROADCAST
;
2553 else if (MATCH("host", argv
[a
]))
2557 cli_print(cli
, "Specify host ip address");
2561 if (!inet_aton(argv
[a
], &addr
))
2563 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2575 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2579 if (!inet_aton(argv
[a
], &addr
))
2581 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2587 if (!inet_aton(argv
[++a
], &addr
))
2589 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2593 *wild
= addr
.s_addr
;
2597 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2601 if (MATCH("eq", argv
[a
]))
2602 port
->op
= FILTER_PORT_OP_EQ
;
2603 else if (MATCH("neq", argv
[a
]))
2604 port
->op
= FILTER_PORT_OP_NEQ
;
2605 else if (MATCH("gt", argv
[a
]))
2606 port
->op
= FILTER_PORT_OP_GT
;
2607 else if (MATCH("lt", argv
[a
]))
2608 port
->op
= FILTER_PORT_OP_LT
;
2609 else if (MATCH("range", argv
[a
]))
2610 port
->op
= FILTER_PORT_OP_RANGE
;
2617 cli_print(cli
, "Specify port");
2621 if (!(port
->port
= atoi(argv
[a
])))
2623 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2628 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2633 cli_print(cli
, "Specify port");
2637 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2639 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2646 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2648 if (MATCH("established", argv
[a
]))
2650 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2653 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2654 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2656 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2657 ? FILTER_FLAG_OP_ANY
2658 : FILTER_FLAG_OP_ALL
;
2662 cli_print(cli
, "Specify tcp flags");
2666 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2670 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2672 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2673 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2674 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2675 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2676 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2677 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2680 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2689 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2691 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2693 cli_print(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2703 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2710 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2712 static ip_filter_rulet rule
;
2713 struct in_addr addr
;
2715 if (CLI_HELP_REQUESTED
)
2719 cli_arg_help(cli
, argv
[0][1],
2720 "A.B.C.D", "Source address",
2721 "any", "Any source address",
2722 "host", "Source host",
2728 if (MATCH("any", argv
[0]))
2730 if (argc
== 2 && !argv
[1][1])
2731 cli_arg_help(cli
, 1, NULL
);
2733 else if (MATCH("host", argv
[0]))
2737 cli_arg_help(cli
, argv
[1][1],
2738 "A.B.C.D", "Host address",
2741 else if (argc
== 3 && !argv
[2][1])
2742 cli_arg_help(cli
, 1, NULL
);
2748 cli_arg_help(cli
, 1,
2749 "A.B.C.D", "Wildcard bits",
2752 else if (argc
== 3 && !argv
[2][1])
2753 cli_arg_help(cli
, 1, NULL
);
2761 cli_print(cli
, "Specify rule details");
2765 memset(&rule
, 0, sizeof(rule
));
2766 rule
.action
= (command
[0] == 'p')
2767 ? FILTER_ACTION_PERMIT
2768 : FILTER_ACTION_DENY
;
2770 rule
.proto
= IPPROTO_IP
;
2771 if (MATCH("any", argv
[0]))
2773 rule
.src_ip
= INADDR_ANY
;
2774 rule
.src_wild
= INADDR_BROADCAST
;
2776 else if (MATCH("host", argv
[0]))
2780 cli_print(cli
, "Specify host ip address");
2784 if (!inet_aton(argv
[1], &addr
))
2786 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2790 rule
.src_ip
= addr
.s_addr
;
2791 rule
.src_wild
= INADDR_ANY
;
2797 cli_print(cli
, "Specify source ip address and wildcard");
2801 if (!inet_aton(argv
[0], &addr
))
2803 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2807 rule
.src_ip
= addr
.s_addr
;
2811 if (!inet_aton(argv
[1], &addr
))
2813 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2817 rule
.src_wild
= addr
.s_addr
;
2820 rule
.src_wild
= INADDR_ANY
;
2826 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2829 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2830 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2831 : access_list_rule_std(cli
, command
, argv
, argc
);
2836 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2838 if (!ip_filters
[filt
].rules
[i
].action
)
2840 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2844 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2848 cli_print(cli
, "Too many rules");
2852 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2857 /* filter USER {in|out} FILTER ... */
2858 if (CLI_HELP_REQUESTED
)
2863 return cli_arg_help(cli
, 0,
2864 "USER", "Username of session to filter", NULL
);
2868 return cli_arg_help(cli
, 0,
2869 "in", "Set incoming filter",
2870 "out", "Set outgoing filter", NULL
);
2874 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2875 "NAME", "Filter name", NULL
);
2878 return cli_arg_help(cli
, argc
> 1, NULL
);
2882 if (!config
->cluster_iam_master
)
2884 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2885 fmtaddr(config
->cluster_master_address
, 0));
2890 if (argc
!= 3 && argc
!= 5)
2892 cli_print(cli
, "Specify a user and filters");
2896 if (!(s
= sessionbyuser(argv
[0])))
2898 cli_print(cli
, "User %s is not connected", argv
[0]);
2902 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2903 for (i
= 1; i
< argc
; i
+= 2)
2908 if (MATCH("in", argv
[i
]))
2910 if (session
[s
].filter_in
)
2912 cli_print(cli
, "Input already filtered");
2915 f
= &cli_session_actions
[s
].filter_in
;
2917 else if (MATCH("out", argv
[i
]))
2919 if (session
[s
].filter_out
)
2921 cli_print(cli
, "Output already filtered");
2924 f
= &cli_session_actions
[s
].filter_out
;
2928 cli_print(cli
, "Invalid filter specification");
2932 v
= find_access_list(argv
[i
+1]);
2933 if (v
< 0 || !*ip_filters
[v
].name
)
2935 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2942 cli_print(cli
, "Filtering user %s", argv
[0]);
2943 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2948 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2953 if (CLI_HELP_REQUESTED
)
2954 return cli_arg_help(cli
, argc
> 1,
2955 "USER", "Username of session to remove filters from", NULL
);
2957 if (!config
->cluster_iam_master
)
2959 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2960 fmtaddr(config
->cluster_master_address
, 0));
2967 cli_print(cli
, "Specify a user to remove filters from");
2971 for (i
= 0; i
< argc
; i
++)
2973 if (!(s
= sessionbyuser(argv
[i
])))
2975 cli_print(cli
, "User %s is not connected", argv
[i
]);
2979 if (session
[s
].filter_in
|| session
[s
].filter_out
)
2981 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
2982 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
2986 cli_print(cli
, "User %s not filtered", argv
[i
]);
2993 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2997 if (CLI_HELP_REQUESTED
)
2998 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3002 cli_print(cli
, "Specify a filter name");
3006 for (i
= 0; i
< argc
; i
++)
3008 int f
= find_access_list(argv
[i
]);
3009 ip_filter_rulet
*rules
;
3011 if (f
< 0 || !*ip_filters
[f
].name
)
3013 cli_print(cli
, "Access-list %s not defined", argv
[i
]);
3020 cli_print(cli
, "%s IP access list %s",
3021 ip_filters
[f
].extended
? "Extended" : "Standard",
3022 ip_filters
[f
].name
);
3024 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3026 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3028 cli_print(cli
, "%s (%d match%s)", r
,
3029 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3031 cli_print(cli
, "%s", r
);
3038 // Convert a string in the form of abcd.ef12.3456 into char[6]
3039 void parsemac(char *string
, char mac
[6])
3041 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)
3043 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)