1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.43.2.4 2005/01/13 08:03:14 bodea Exp $";
18 #include <arpa/inet.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
36 extern tunnelt
*tunnel
;
37 extern sessiont
*session
;
38 extern radiust
*radius
;
39 extern ippoolt
*ip_address_pool
;
40 extern struct Tstats
*_statistics
;
41 static struct cli_def
*cli
= NULL
;
42 extern configt
*config
;
43 extern config_descriptt config_values
[];
45 extern struct Tringbuffer
*ringbuffer
;
47 extern struct cli_session_actions
*cli_session_actions
;
48 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
49 extern tbft
*filter_list
;
50 extern ip_filtert
*ip_filters
;
52 static char *debug_levels
[] = {
71 static int debug_session
;
72 static int debug_tunnel
;
73 static int debug_rb_tail
;
75 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 static int regular_stuff(struct cli_def
*cli
);
102 static void parsemac(char *string
, char mac
[6]);
105 #define MODE_CONFIG_BGP 8
106 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
107 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
110 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
111 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
112 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
115 #define MODE_CONFIG_NACL 9
116 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
120 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
121 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
123 /* match if b is a substr of a */
124 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
126 void init_cli(char *hostname
)
130 struct cli_command
*c
;
131 struct cli_command
*c2
;
133 struct sockaddr_in addr
;
136 if (hostname
&& *hostname
)
137 cli_set_hostname(cli
, hostname
);
139 cli_set_hostname(cli
, "l2tpns");
141 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
142 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
144 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
146 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
147 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
148 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
149 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
150 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
151 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
152 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
153 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
154 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
155 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
156 cli_register_command(cli
, c
, "users", cmd_show_users
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of all connected users or details of selected user");
157 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
158 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
160 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
161 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
162 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
165 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
167 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
168 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
171 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
173 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
174 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
175 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
177 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
178 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
179 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
180 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
183 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
184 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
187 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
188 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
189 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
190 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
191 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
194 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
195 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
197 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
198 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
200 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
201 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
203 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
205 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
206 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
209 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
210 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
211 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
212 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
214 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
215 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
217 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
218 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
220 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
222 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
223 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
225 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
226 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
228 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
229 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
230 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
232 // Enable regular processing
233 cli_regular(cli
, regular_stuff
);
235 if (!(f
= fopen(CLIUSERS
, "r")))
237 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
241 while (fgets(buf
, 4096, f
))
244 if (*buf
== '#') continue;
245 if ((p
= strchr(buf
, '\r'))) *p
= 0;
246 if ((p
= strchr(buf
, '\n'))) *p
= 0;
248 if (!(p
= strchr((char *)buf
, ':'))) continue;
250 if (!strcmp(buf
, "enable"))
252 cli_allow_enable(cli
, p
);
253 LOG(3, 0, 0, "Setting enable password\n");
257 cli_allow_user(cli
, buf
, p
);
258 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
264 memset(&addr
, 0, sizeof(addr
));
265 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
266 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
269 // Set cli fd as non-blocking
270 flags
= fcntl(clifd
, F_GETFL
, 0);
271 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
273 addr
.sin_family
= AF_INET
;
274 addr
.sin_port
= htons(23);
275 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
277 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
283 void cli_do(int sockfd
)
285 int require_auth
= 1;
286 struct sockaddr_in addr
;
287 int l
= sizeof(addr
);
289 if (fork_and_close()) return;
290 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
292 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
293 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
294 fmtaddr(addr
.sin_addr
.s_addr
, 0));
297 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
301 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
302 if (!cli
->users
) /* paranoia */
304 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
310 /* no username/pass required */
317 debug_rb_tail
= ringbuffer
->tail
;
319 memset(&debug_flags
, 0, sizeof(debug_flags
));
320 debug_flags
.critical
= 1;
322 cli_loop(cli
, sockfd
);
325 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
326 fmtaddr(addr
.sin_addr
.s_addr
, 0));
331 static void cli_print_log(struct cli_def
*cli
, char *string
)
333 LOG(3, 0, 0, "%s\n", string
);
336 void cli_do_file(FILE *fh
)
338 LOG(3, 0, 0, "Reading configuration file\n");
339 cli_print_callback(cli
, cli_print_log
);
340 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
341 cli_print_callback(cli
, NULL
);
344 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
355 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
357 int v
= va_arg(ap
, int);
358 snprintf(buf
, sizeof(buf
), entry
, v
);
364 desc
= va_arg(ap
, char *);
366 cli_print(cli
, " %-20s %s", p
, desc
);
368 cli_print(cli
, " %s", p
);
370 entry
= desc
? va_arg(ap
, char *) : 0;
375 cli_print(cli
, " <cr>");
380 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
384 if (CLI_HELP_REQUESTED
)
385 return cli_arg_help(cli
, 1,
386 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
392 // Show individual session
393 for (i
= 0; i
< argc
; i
++)
395 unsigned int s
, b_in
, b_out
;
397 if (s
<= 0 || s
>= MAXSESSION
)
399 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
402 cli_print(cli
, "\r\nSession %d:", s
);
403 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
404 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
405 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
406 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
407 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
408 cli_print(cli
, "\tUnique SID:\t%lu", session
[s
].unique_id
);
409 cli_print(cli
, "\tIdle time:\t%u seconds", abs(time_now
- session
[s
].last_packet
));
410 cli_print(cli
, "\tNext Recv:\t%u", session
[s
].nr
);
411 cli_print(cli
, "\tNext Send:\t%u", session
[s
].ns
);
412 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].total_cout
, session
[s
].total_cin
);
413 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
414 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
415 cli_print(cli
, "\tRadius Session:\t%u", session
[s
].radius
);
416 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
417 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
418 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
419 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
420 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
421 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
422 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
423 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
425 cli_print(cli
, "\tIntercepted:\tno");
427 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
429 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
430 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
431 t
? "YES" : "no", t
? " (" : "",
432 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
434 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
438 b_in
= session
[s
].tbf_in
;
439 b_out
= session
[s
].tbf_out
;
441 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
442 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
443 "ByteSent", "PackSent", "PackDrop", "PackDelay");
446 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
448 (filter_list
[b_in
].next
? "*" : " "),
449 (b_in
< 100 ? "\t" : ""),
450 filter_list
[b_in
].rate
* 8,
451 filter_list
[b_in
].credit
,
452 filter_list
[b_in
].queued
,
453 filter_list
[b_in
].b_queued
,
454 filter_list
[b_in
].p_queued
,
455 filter_list
[b_in
].b_sent
,
456 filter_list
[b_in
].p_sent
,
457 filter_list
[b_in
].p_dropped
,
458 filter_list
[b_in
].p_delayed
);
461 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
463 (filter_list
[b_out
].next
? "*" : " "),
464 (b_out
< 100 ? "\t" : ""),
465 filter_list
[b_out
].rate
* 8,
466 filter_list
[b_out
].credit
,
467 filter_list
[b_out
].queued
,
468 filter_list
[b_out
].b_queued
,
469 filter_list
[b_out
].p_queued
,
470 filter_list
[b_out
].b_sent
,
471 filter_list
[b_out
].p_sent
,
472 filter_list
[b_out
].p_dropped
,
473 filter_list
[b_out
].p_delayed
);
480 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
495 for (i
= 1; i
< MAXSESSION
; i
++)
497 if (!session
[i
].opened
) continue;
498 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
501 session
[i
].user
[0] ? session
[i
].user
: "*",
502 fmtaddr(htonl(session
[i
].ip
), 0),
503 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
504 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
505 (session
[i
].walled_garden
) ? "Y" : "N",
506 abs(time_now
- (unsigned long)session
[i
].opened
),
507 (unsigned long)session
[i
].total_cout
,
508 (unsigned long)session
[i
].total_cin
,
509 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
510 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
511 session
[i
].calling
[0] ? session
[i
].calling
: "*");
516 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
518 int i
, x
, show_all
= 0;
526 if (CLI_HELP_REQUESTED
)
529 return cli_arg_help(cli
, 1,
530 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
533 return cli_arg_help(cli
, 1,
534 "all", "Show all tunnels, including unused",
535 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
542 if (strcmp(argv
[0], "all") == 0)
548 // Show individual tunnel
549 for (i
= 0; i
< argc
; i
++)
554 if (t
<= 0 || t
>= MAXTUNNEL
)
556 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
559 cli_print(cli
, "\r\nTunnel %d:", t
);
560 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
561 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
562 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
563 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
564 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
565 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
566 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
567 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
568 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
570 for (x
= 0; x
< MAXSESSION
; x
++)
571 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
572 sprintf(s
, "%s%u ", s
, x
);
574 cli_print(cli
, "\tSessions:\t%s", s
);
580 // Show tunnel summary
581 cli_print(cli
, "%4s %20s %20s %6s %s",
588 for (i
= 1; i
< MAXTUNNEL
; i
++)
591 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
593 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
594 cli_print(cli
, "%4d %20s %20s %6s %6d",
596 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
597 fmtaddr(htonl(tunnel
[i
].ip
), 0),
598 states
[tunnel
[i
].state
],
605 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
612 if (CLI_HELP_REQUESTED
)
613 return cli_arg_help(cli
, 1,
614 "USER", "Show details for specific username",
617 for (i
= 0; i
< MAXSESSION
; i
++)
619 if (!session
[i
].opened
) continue;
620 if (!session
[i
].user
[0]) continue;
624 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
626 if (strcmp(argv
[j
], session
[i
].user
) == 0)
628 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
629 sargv
[sargc
] = sid
[sargc
];
637 cli_print(cli
, "%s", session
[i
].user
);
641 return cmd_show_session(cli
, "users", sargv
, sargc
);
646 static char *duration(time_t secs
)
648 static char *buf
= NULL
;
651 if (!buf
) buf
= calloc(64, 1);
655 int days
= secs
/ 86400;
656 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
662 int mins
= secs
/ 60;
666 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
670 int mins
= secs
/ 60;
671 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
674 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
679 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
681 if (CLI_HELP_REQUESTED
)
682 return CLI_HELP_NO_ARGS
;
684 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
685 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
686 GET_STAT(tun_rx_bytes
),
687 GET_STAT(tun_rx_packets
),
688 GET_STAT(tun_rx_errors
),
689 GET_STAT(tun_rx_dropped
));
690 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
691 GET_STAT(tun_tx_bytes
),
692 GET_STAT(tun_tx_packets
),
693 GET_STAT(tun_tx_errors
));
696 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
697 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
698 GET_STAT(tunnel_rx_bytes
),
699 GET_STAT(tunnel_rx_packets
),
700 GET_STAT(tunnel_rx_errors
));
701 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
702 GET_STAT(tunnel_tx_bytes
),
703 GET_STAT(tunnel_tx_packets
),
704 GET_STAT(tunnel_tx_errors
),
705 GET_STAT(tunnel_retries
));
708 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
709 cli_print(cli
, "-----------------------------------------");
710 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
711 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
712 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
713 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
714 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
715 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
716 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
717 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
718 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
719 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
720 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
721 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
722 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
723 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
724 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
725 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
726 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
727 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
731 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
732 cli_print(cli
, "-----------------------------------------");
733 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
734 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
735 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
736 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
737 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
738 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
739 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
740 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
741 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
742 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
743 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
744 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
745 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
746 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
747 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
748 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
749 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
750 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
751 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
752 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
753 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
754 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
755 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
756 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
757 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
758 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
762 cli_print(cli
, "Counters last reset %s ago", duration(time_now
- GET_STAT(last_reset
)));
767 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
773 if (CLI_HELP_REQUESTED
)
774 return cli_arg_help(cli
, 1,
775 "tag", "Include CVS release tag",
776 "file", "Include file versions",
779 for (i
= 0; i
< argc
; i
++)
780 if (!strcmp(argv
[i
], "tag"))
782 else if (!strcmp(argv
[i
], "file"))
785 cli_print(cli
, "L2TPNS %s", VERSION
);
788 char const *p
= strchr(cvs_name
, ':');
800 e
= strpbrk(p
, " \t$");
801 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
806 extern linked_list
*loaded_plugins
;
809 cli_print(cli
, "Files:");
810 cli_print(cli
, " %s", cvs_id_arp
);
812 cli_print(cli
, " %s", cvs_id_bgp
);
814 cli_print(cli
, " %s", cvs_id_cli
);
815 cli_print(cli
, " %s", cvs_id_cluster
);
816 cli_print(cli
, " %s", cvs_id_constants
);
817 cli_print(cli
, " %s", cvs_id_control
);
818 cli_print(cli
, " %s", cvs_id_icmp
);
819 cli_print(cli
, " %s", cvs_id_l2tpns
);
820 cli_print(cli
, " %s", cvs_id_ll
);
821 cli_print(cli
, " %s", cvs_id_md5
);
822 cli_print(cli
, " %s", cvs_id_ppp
);
823 cli_print(cli
, " %s", cvs_id_radius
);
824 cli_print(cli
, " %s", cvs_id_tbf
);
825 cli_print(cli
, " %s", cvs_id_util
);
827 ll_reset(loaded_plugins
);
828 while ((p
= ll_next(loaded_plugins
)))
830 char const **id
= dlsym(p
, "cvs_id");
832 cli_print(cli
, " %s", *id
);
839 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
842 int used
= 0, free
= 0, show_all
= 0;
844 if (!config
->cluster_iam_master
)
846 cli_print(cli
, "Can't do this on a slave. Do it on %s",
847 fmtaddr(config
->cluster_master_address
, 0));
852 if (CLI_HELP_REQUESTED
)
855 return cli_arg_help(cli
, 1, NULL
);
857 return cli_arg_help(cli
, 1,
858 "all", "Show all pool addresses, including unused",
862 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
866 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
867 for (i
= 0; i
< MAXIPPOOL
; i
++)
869 if (!ip_address_pool
[i
].address
) continue;
870 if (ip_address_pool
[i
].assigned
)
872 cli_print(cli
, "%-15s\tY %8d %s",
873 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
874 ip_address_pool
[i
].session
,
875 session
[ip_address_pool
[i
].session
].user
);
881 if (ip_address_pool
[i
].last
)
882 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
883 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
884 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
887 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
894 cli_print(cli
, "(Not displaying unused addresses)");
896 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
900 static FILE *save_config_fh
= 0;
901 static void print_save_config(struct cli_def
*cli
, char *string
)
904 fprintf(save_config_fh
, "%s\n", string
);
907 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
909 if (CLI_HELP_REQUESTED
)
910 return CLI_HELP_NO_ARGS
;
912 if ((save_config_fh
= fopen(config
->config_file
, "w")))
914 cli_print(cli
, "Writing configuration");
915 cli_print_callback(cli
, print_save_config
);
916 cmd_show_run(cli
, command
, argv
, argc
);
917 cli_print_callback(cli
, NULL
);
918 fclose(save_config_fh
);
923 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
928 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
930 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
934 if (CLI_HELP_REQUESTED
)
935 return CLI_HELP_NO_ARGS
;
937 cli_print(cli
, "# Current configuration:");
939 for (i
= 0; config_values
[i
].key
; i
++)
941 void *value
= ((void *)config
) + config_values
[i
].offset
;
942 if (config_values
[i
].type
== STRING
)
943 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
944 else if (config_values
[i
].type
== IP
)
945 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(unsigned *)value
, 0));
946 else if (config_values
[i
].type
== SHORT
)
947 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
948 else if (config_values
[i
].type
== BOOL
)
949 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
950 else if (config_values
[i
].type
== INT
)
951 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
952 else if (config_values
[i
].type
== UNSIGNED_LONG
)
953 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
954 else if (config_values
[i
].type
== MAC
)
955 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
956 *(unsigned short *)(value
+ 0),
957 *(unsigned short *)(value
+ 1),
958 *(unsigned short *)(value
+ 2),
959 *(unsigned short *)(value
+ 3),
960 *(unsigned short *)(value
+ 4),
961 *(unsigned short *)(value
+ 5));
964 cli_print(cli
, "# Plugins");
965 for (i
= 0; i
< MAXPLUGINS
; i
++)
967 if (*config
->plugins
[i
])
969 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
974 if (config
->as_number
)
979 cli_print(cli
, "# BGP");
980 cli_print(cli
, "router bgp %u", config
->as_number
);
981 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
983 if (!config
->neighbour
[i
].name
[0])
986 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
988 k
= config
->neighbour
[i
].keepalive
;
989 h
= config
->neighbour
[i
].hold
;
996 k
= BGP_KEEPALIVE_TIME
;
1002 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1007 cli_print(cli
, "# Filters");
1008 for (i
= 0; i
< MAXFILTER
; i
++)
1010 ip_filter_rulet
*rules
;
1011 if (!*ip_filters
[i
].name
)
1014 cli_print(cli
, "ip access-list %s %s",
1015 ip_filters
[i
].extended
? "extended" : "standard",
1016 ip_filters
[i
].name
);
1018 rules
= ip_filters
[i
].rules
;
1019 while (rules
->action
)
1020 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1023 cli_print(cli
, "# end");
1027 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1029 int i
, free
= 0, used
= 0, show_all
= 0;
1040 if (CLI_HELP_REQUESTED
)
1043 return cli_arg_help(cli
, 1, NULL
);
1045 return cli_arg_help(cli
, 1,
1046 "all", "Show all RADIUS sessions, including unused",
1050 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1054 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1057 for (i
= 1; i
< MAXRADIUS
; i
++)
1059 if (radius
[i
].state
== RADIUSNULL
)
1064 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1066 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1070 states
[radius
[i
].state
],
1076 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1081 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1085 if (CLI_HELP_REQUESTED
)
1086 return CLI_HELP_NO_ARGS
;
1088 cli_print(cli
, "Plugins currently loaded:");
1089 for (i
= 0; i
< MAXPLUGINS
; i
++)
1090 if (*config
->plugins
[i
])
1091 cli_print(cli
, " %s", config
->plugins
[i
]);
1096 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1100 if (CLI_HELP_REQUESTED
)
1101 return CLI_HELP_NO_ARGS
;
1103 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1112 for (i
= 0; i
< MAXSESSION
; i
++)
1114 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1115 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1119 session
[i
].throttle_in
,
1120 session
[i
].throttle_out
,
1122 session
[i
].tbf_out
);
1128 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1130 if (CLI_HELP_REQUESTED
)
1131 return CLI_HELP_NO_ARGS
;
1133 cli_print(cli
, " _\n"
1139 " ( \\ `. `-. _,.-:\\\n"
1140 " \\ \\ `. `-._ __..--' ,-';/\n"
1141 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1142 " `. `. `-._ __..--' ,' /\n"
1143 " `. `-_ ``--..'' _.-' ,'\n"
1144 " `-_ `-.___ __,--' ,'\n"
1145 " `-.__ `----\"\"\" __.-'\n"
1146 "hh `--..____..--'");
1151 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1153 if (CLI_HELP_REQUESTED
)
1154 return CLI_HELP_NO_ARGS
;
1156 memset(_statistics
, 0, sizeof(struct Tstats
));
1157 SET_STAT(last_reset
, time(NULL
));
1159 cli_print(cli
, "Counters cleared");
1163 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1168 if (CLI_HELP_REQUESTED
)
1169 return cli_arg_help(cli
, argc
> 1,
1170 "USER", "Username of session to drop", NULL
);
1172 if (!config
->cluster_iam_master
)
1174 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1175 fmtaddr(config
->cluster_master_address
, 0));
1182 cli_print(cli
, "Specify a user to drop");
1186 for (i
= 0; i
< argc
; i
++)
1188 if (!(s
= sessionbyuser(argv
[i
])))
1190 cli_print(cli
, "User %s is not connected", argv
[i
]);
1194 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1196 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1197 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1204 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1209 if (CLI_HELP_REQUESTED
)
1210 return cli_arg_help(cli
, argc
> 1,
1211 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1213 if (!config
->cluster_iam_master
)
1215 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1216 fmtaddr(config
->cluster_master_address
, 0));
1223 cli_print(cli
, "Specify a tunnel to drop");
1227 for (i
= 0; i
< argc
; i
++)
1229 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1231 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1237 cli_print(cli
, "Tunnel %d is not connected", t
);
1243 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1247 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1248 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1254 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1259 if (CLI_HELP_REQUESTED
)
1260 return cli_arg_help(cli
, argc
> 1,
1261 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1263 if (!config
->cluster_iam_master
)
1265 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1266 fmtaddr(config
->cluster_master_address
, 0));
1273 cli_print(cli
, "Specify a session id to drop");
1277 for (i
= 0; i
< argc
; i
++)
1279 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1281 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1285 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1287 cli_print(cli
, "Dropping session %d", s
);
1288 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1292 cli_print(cli
, "Session %d is not active.", s
);
1299 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1305 if (CLI_HELP_REQUESTED
)
1310 return cli_arg_help(cli
, 0,
1311 "USER", "Username of session to snoop", NULL
);
1314 return cli_arg_help(cli
, 0,
1315 "A.B.C.D", "IP address of snoop destination", NULL
);
1318 return cli_arg_help(cli
, 0,
1319 "N", "Port of snoop destination", NULL
);
1323 return cli_arg_help(cli
, 1, NULL
);
1330 if (!config
->cluster_iam_master
)
1332 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1333 fmtaddr(config
->cluster_master_address
, 0));
1340 cli_print(cli
, "Specify username, ip and port");
1344 if (!(s
= sessionbyuser(argv
[0])))
1346 cli_print(cli
, "User %s is not connected", argv
[0]);
1350 ip
= inet_addr(argv
[1]);
1351 if (!ip
|| ip
== INADDR_NONE
)
1353 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1357 port
= atoi(argv
[2]);
1360 cli_print(cli
, "Invalid port %s", argv
[2]);
1364 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1365 cli_session_actions
[s
].snoop_ip
= ip
;
1366 cli_session_actions
[s
].snoop_port
= port
;
1367 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1372 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1377 if (CLI_HELP_REQUESTED
)
1378 return cli_arg_help(cli
, argc
> 1,
1379 "USER", "Username of session to unsnoop", NULL
);
1381 if (!config
->cluster_iam_master
)
1383 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1384 fmtaddr(config
->cluster_master_address
, 0));
1391 cli_print(cli
, "Specify a user to unsnoop");
1395 for (i
= 0; i
< argc
; i
++)
1397 if (!(s
= sessionbyuser(argv
[i
])))
1399 cli_print(cli
, "User %s is not connected", argv
[i
]);
1403 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1404 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1410 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1417 throttle USER - throttle in/out to default rate
1418 throttle USER RATE - throttle in/out to default rate
1419 throttle USER in RATE - throttle input only
1420 throttle USER out RATE - throttle output only
1421 throttle USER in RATE out RATE - throttle both
1424 if (CLI_HELP_REQUESTED
)
1429 return cli_arg_help(cli
, 0,
1430 "USER", "Username of session to throttle", NULL
);
1433 return cli_arg_help(cli
, 1,
1434 "RATE", "Rate in kbps (in and out)",
1435 "in", "Select incoming rate",
1436 "out", "Select outgoing rate", NULL
);
1439 return cli_arg_help(cli
, 1,
1440 "in", "Select incoming rate",
1441 "out", "Select outgoing rate", NULL
);
1444 if (isdigit(argv
[1][0]))
1445 return cli_arg_help(cli
, 1, NULL
);
1448 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1451 return cli_arg_help(cli
, argc
> 1, NULL
);
1455 if (!config
->cluster_iam_master
)
1457 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1458 fmtaddr(config
->cluster_master_address
, 0));
1465 cli_print(cli
, "Specify a user to throttle");
1469 if (!(s
= sessionbyuser(argv
[0])))
1471 cli_print(cli
, "User %s is not connected", argv
[0]);
1477 rate_in
= rate_out
= config
->rl_rate
;
1481 rate_in
= rate_out
= atoi(argv
[1]);
1484 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1488 else if (argc
== 3 || argc
== 5)
1491 for (i
= 1; i
< argc
- 1; i
+= 2)
1494 if (MATCH("in", argv
[i
]))
1495 r
= rate_in
= atoi(argv
[i
+1]);
1496 else if (MATCH("out", argv
[i
]))
1497 r
= rate_out
= atoi(argv
[i
+1]);
1501 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1508 cli_print(cli
, "Invalid arguments");
1512 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1514 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1518 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1519 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1520 cli_session_actions
[s
].throttle_in
= rate_in
;
1522 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1523 cli_session_actions
[s
].throttle_out
= rate_out
;
1525 if (cli_session_actions
[s
].throttle_in
== -1 &&
1526 cli_session_actions
[s
].throttle_out
== -1)
1528 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1532 cli_print(cli
, "Throttling user %s", argv
[0]);
1533 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1538 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1543 if (CLI_HELP_REQUESTED
)
1544 return cli_arg_help(cli
, argc
> 1,
1545 "USER", "Username of session to unthrottle", NULL
);
1547 if (!config
->cluster_iam_master
)
1549 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1550 fmtaddr(config
->cluster_master_address
, 0));
1557 cli_print(cli
, "Specify a user to unthrottle");
1561 for (i
= 0; i
< argc
; i
++)
1563 if (!(s
= sessionbyuser(argv
[i
])))
1565 cli_print(cli
, "User %s is not connected", argv
[i
]);
1569 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1571 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1572 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1576 cli_print(cli
, "User %s not throttled", argv
[i
]);
1583 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1587 if (CLI_HELP_REQUESTED
)
1588 return cli_arg_help(cli
, 1,
1589 "all", "Enable debugging for all except \"data\"",
1590 "critical", "", // FIXME: add descriptions
1600 char *p
= (char *) &debug_flags
;
1601 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1605 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1606 (debug_flags
.critical
) ? " critical" : "",
1607 (debug_flags
.error
) ? " error" : "",
1608 (debug_flags
.warning
) ? " warning" : "",
1609 (debug_flags
.info
) ? " info" : "",
1610 (debug_flags
.calls
) ? " calls" : "",
1611 (debug_flags
.data
) ? " data" : "");
1617 cli_print(cli
, "Debugging off");
1621 for (i
= 0; i
< argc
; i
++)
1623 int len
= strlen(argv
[i
]);
1625 if (argv
[i
][0] == 'c' && len
< 2)
1626 len
= 2; /* distinguish [cr]itical from [ca]lls */
1628 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1629 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1630 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1631 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1632 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1633 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1634 if (!strncmp("all", argv
[i
], len
))
1636 memset(&debug_flags
, 1, sizeof(debug_flags
));
1637 debug_flags
.data
= 0;
1641 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1647 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1651 if (CLI_HELP_REQUESTED
)
1652 return cli_arg_help(cli
, 1,
1653 "all", "Disable all debugging",
1654 "critical", "", // FIXME: add descriptions
1664 memset(&debug_flags
, 0, sizeof(debug_flags
));
1668 for (i
= 0; i
< argc
; i
++)
1670 int len
= strlen(argv
[i
]);
1672 if (argv
[i
][0] == 'c' && len
< 2)
1673 len
= 2; /* distinguish [cr]itical from [ca]lls */
1675 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1676 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1677 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1678 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1679 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1680 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1681 if (!strncmp("all", argv
[i
], len
))
1683 memset(&debug_flags
, 0, sizeof(debug_flags
));
1687 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1693 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1695 int i
, firstfree
= 0;
1697 if (CLI_HELP_REQUESTED
)
1698 return cli_arg_help(cli
, argc
> 1,
1699 "PLUGIN", "Name of plugin to load", NULL
);
1703 cli_print(cli
, "Specify a plugin to load");
1707 for (i
= 0; i
< MAXPLUGINS
; i
++)
1709 if (!*config
->plugins
[i
] && !firstfree
)
1711 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1713 cli_print(cli
, "Plugin is already loaded");
1720 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1721 config
->reload_config
= 1;
1722 cli_print(cli
, "Loading plugin %s", argv
[0]);
1728 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1732 if (CLI_HELP_REQUESTED
)
1733 return cli_arg_help(cli
, argc
> 1,
1734 "PLUGIN", "Name of plugin to unload", NULL
);
1738 cli_print(cli
, "Specify a plugin to remove");
1742 for (i
= 0; i
< MAXPLUGINS
; i
++)
1744 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1746 config
->reload_config
= 1;
1747 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1752 cli_print(cli
, "Plugin is not loaded");
1756 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1759 char buf
[100], *p
= buf
, *loads
[3];
1760 int i
, num_sessions
= 0;
1762 if (CLI_HELP_REQUESTED
)
1763 return CLI_HELP_NO_ARGS
;
1765 fh
= fopen("/proc/loadavg", "r");
1766 fgets(buf
, 100, fh
);
1769 for (i
= 0; i
< 3; i
++)
1770 loads
[i
] = strdup(strsep(&p
, " "));
1773 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1775 for (i
= 1; i
< MAXSESSION
; i
++)
1776 if (session
[i
].opened
) num_sessions
++;
1778 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1780 duration(time_now
- config
->start_time
),
1782 loads
[0], loads
[1], loads
[2]
1784 for (i
= 0; i
< 3; i
++)
1785 if (loads
[i
]) free(loads
[i
]);
1787 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1792 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1796 if (CLI_HELP_REQUESTED
)
1802 int len
= strlen(argv
[0])-1;
1803 for (i
= 0; config_values
[i
].key
; i
++)
1804 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1805 cli_print(cli
, " %s", config_values
[i
].key
);
1811 return cli_arg_help(cli
, 0,
1812 "VALUE", "Value for variable", NULL
);
1816 return cli_arg_help(cli
, 1, NULL
);
1825 cli_print(cli
, "Specify variable and value");
1829 for (i
= 0; config_values
[i
].key
; i
++)
1831 void *value
= ((void *)config
) + config_values
[i
].offset
;
1832 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1834 // Found a value to set
1835 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1836 switch (config_values
[i
].type
)
1839 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1842 *(int *)value
= atoi(argv
[1]);
1845 *(unsigned long *)value
= atol(argv
[1]);
1848 *(short *)value
= atoi(argv
[1]);
1851 *(unsigned *)value
= inet_addr(argv
[1]);
1854 parsemac(argv
[1], (char *)value
);
1857 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1863 cli_print(cli
, "Unknown variable type");
1866 config
->reload_config
= 1;
1871 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1875 int regular_stuff(struct cli_def
*cli
)
1881 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1883 char *m
= ringbuffer
->buffer
[i
].message
;
1889 switch (ringbuffer
->buffer
[i
].level
)
1891 case 0: show
= debug_flags
.critical
; break;
1892 case 1: show
= debug_flags
.error
; break;
1893 case 2: show
= debug_flags
.warning
; break;
1894 case 3: show
= debug_flags
.info
; break;
1895 case 4: show
= debug_flags
.calls
; break;
1896 case 5: show
= debug_flags
.data
; break;
1899 if (!show
) continue;
1901 if (!(p
= strchr(m
, '\n')))
1904 cli_print(cli
, "\r%s-%u-%u %.*s",
1905 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1906 ringbuffer
->buffer
[i
].tunnel
,
1907 ringbuffer
->buffer
[i
].session
,
1913 debug_rb_tail
= ringbuffer
->tail
;
1921 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1925 if (CLI_HELP_REQUESTED
)
1926 return cli_arg_help(cli
, argc
> 1,
1927 "<1-65535>", "Autonomous system number", NULL
);
1929 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1931 cli_print(cli
, "Invalid autonomous system number");
1935 if (bgp_configured
&& as
!= config
->as_number
)
1937 cli_print(cli
, "Can't change local AS on a running system");
1941 config
->as_number
= as
;
1942 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1947 static int find_bgp_neighbour(char const *name
)
1952 in_addr_t addrs
[4] = { 0 };
1955 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1958 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1959 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1961 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1963 if (!config
->neighbour
[i
].name
[0])
1965 if (new == -1) new = i
;
1969 if (!strcmp(name
, config
->neighbour
[i
].name
))
1972 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1975 for (a
= h
->h_addr_list
; *a
; a
++)
1978 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1979 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1987 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1993 if (CLI_HELP_REQUESTED
)
1998 return cli_arg_help(cli
, 0,
1999 "A.B.C.D", "BGP neighbour address",
2000 "NAME", "BGP neighbour name",
2004 return cli_arg_help(cli
, 0,
2005 "remote-as", "Set remote autonomous system number",
2006 "timers", "Set timers",
2010 if (MATCH("remote-as", argv
[1]))
2011 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2013 if (MATCH("timers", argv
[1]))
2016 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2019 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2021 if (argc
== 5 && !argv
[4][1])
2022 return cli_arg_help(cli
, 1, NULL
);
2031 cli_print(cli
, "Invalid arguments");
2035 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2037 cli_print(cli
, "Invalid neighbour");
2043 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2047 if (MATCH("remote-as", argv
[1]))
2049 int as
= atoi(argv
[2]);
2050 if (as
< 0 || as
> 65535)
2052 cli_print(cli
, "Invalid autonomous system number");
2056 if (!config
->neighbour
[i
].name
[0])
2058 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2059 config
->neighbour
[i
].keepalive
= -1;
2060 config
->neighbour
[i
].hold
= -1;
2063 config
->neighbour
[i
].as
= as
;
2067 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2069 cli_print(cli
, "Invalid arguments");
2073 if (!config
->neighbour
[i
].name
[0])
2075 cli_print(cli
, "Specify remote-as first");
2079 keepalive
= atoi(argv
[2]);
2080 hold
= atoi(argv
[3]);
2082 if (keepalive
< 1 || keepalive
> 65535)
2084 cli_print(cli
, "Invalid keepalive time");
2088 if (hold
< 3 || hold
> 65535)
2090 cli_print(cli
, "Invalid hold time");
2094 if (keepalive
== BGP_KEEPALIVE_TIME
)
2095 keepalive
= -1; // using default value
2097 if (hold
== BGP_HOLD_TIME
)
2100 config
->neighbour
[i
].keepalive
= keepalive
;
2101 config
->neighbour
[i
].hold
= hold
;
2106 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2110 if (CLI_HELP_REQUESTED
)
2111 return cli_arg_help(cli
, argc
> 0,
2112 "A.B.C.D", "BGP neighbour address",
2113 "NAME", "BGP neighbour name",
2118 cli_print(cli
, "Specify a BGP neighbour");
2122 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2124 cli_print(cli
, "Invalid neighbour");
2128 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2130 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2134 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2138 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2144 if (!bgp_configured
)
2147 if (CLI_HELP_REQUESTED
)
2148 return cli_arg_help(cli
, 1,
2149 "A.B.C.D", "BGP neighbour address",
2150 "NAME", "BGP neighbour name",
2153 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2154 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2158 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2160 if (!*bgp_peers
[i
].name
)
2163 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2164 if (argc
&& strcmp(addr
, argv
[0]) &&
2165 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2171 cli_print(cli
, "Peer AS Address "
2172 "State Retries Retry in Route Pend Timers");
2173 cli_print(cli
, "------------------ ----- --------------- "
2174 "----------- ------- -------- ----- ---- ---------");
2177 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2181 bgp_state_str(bgp_peers
[i
].state
),
2182 bgp_peers
[i
].retry_count
,
2183 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2184 bgp_peers
[i
].routing
? "yes" : "no",
2185 bgp_peers
[i
].update_routes
? "yes" : "no",
2186 bgp_peers
[i
].keepalive
,
2193 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2198 if (!bgp_configured
)
2201 if (CLI_HELP_REQUESTED
)
2202 return cli_arg_help(cli
, 1,
2203 "A.B.C.D", "BGP neighbour address",
2204 "NAME", "BGP neighbour name",
2207 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2209 if (bgp_peers
[i
].state
!= Established
)
2212 if (!bgp_peers
[i
].routing
)
2215 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2216 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2219 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2220 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2226 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2231 if (!bgp_configured
)
2234 if (CLI_HELP_REQUESTED
)
2235 return cli_arg_help(cli
, 1,
2236 "A.B.C.D", "BGP neighbour address",
2237 "NAME", "BGP neighbour name",
2240 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2242 if (bgp_peers
[i
].state
!= Established
)
2245 if (bgp_peers
[i
].routing
)
2248 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2249 if (argc
&& strcmp(addr
, argv
[0]) &&
2250 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2253 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2254 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2260 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2265 if (!bgp_configured
)
2268 if (CLI_HELP_REQUESTED
)
2269 return cli_arg_help(cli
, 1,
2270 "A.B.C.D", "BGP neighbour address",
2271 "NAME", "BGP neighbour name",
2274 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2276 if (!*bgp_peers
[i
].name
)
2279 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2280 if (argc
&& strcmp(addr
, argv
[0]) &&
2281 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2284 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2285 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2293 static int find_access_list(char const *name
)
2297 for (i
= 0; i
< MAXFILTER
; i
++)
2298 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2304 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2308 if (CLI_HELP_REQUESTED
)
2313 return cli_arg_help(cli
, 0,
2314 "standard", "Standard syntax",
2315 "extended", "Extended syntax",
2319 return cli_arg_help(cli
, argv
[1][1],
2320 "NAME", "Access-list name",
2324 if (argc
== 3 && !argv
[2][1])
2325 return cli_arg_help(cli
, 1, NULL
);
2333 cli_print(cli
, "Specify access-list type and name");
2337 if (MATCH("standard", argv
[0]))
2339 else if (MATCH("extended", argv
[0]))
2343 cli_print(cli
, "Invalid access-list type");
2347 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2348 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2350 cli_print(cli
, "Invalid access-list name");
2354 filt
= find_access_list(argv
[1]);
2359 cli_print(cli
, "Too many access-lists");
2364 if (!*ip_filters
[filt
].name
)
2366 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2367 strcpy(ip_filters
[filt
].name
, argv
[1]);
2368 ip_filters
[filt
].extended
= extended
;
2370 else if (ip_filters
[filt
].extended
!= extended
)
2372 cli_print(cli
, "Access-list is %s",
2373 ip_filters
[filt
].extended
? "extended" : "standard");
2378 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2382 if (filt
< 0 || !*ip_filters
[filt
].name
)
2384 cli_print(cli
, "Access-list not defined");
2389 if (ip_filters
[filt
].used
)
2391 cli_print(cli
, "Access-list in use");
2395 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2399 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2401 return access_list(cli
, argv
, argc
, 1);
2404 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2406 return access_list(cli
, argv
, argc
, 0);
2409 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2411 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2412 return sprintf(buf
, " any");
2414 if (wild
== INADDR_ANY
)
2415 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2417 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2420 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2424 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2425 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2426 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2427 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2428 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2434 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2436 static char buf
[256];
2439 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2442 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2443 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2446 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2450 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2451 p
+= show_ports(p
, &rule
->src_ports
);
2453 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2454 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2455 p
+= show_ports(p
, &rule
->dst_ports
);
2457 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2459 switch (rule
->tcp_flag_op
)
2461 case FILTER_FLAG_OP_EST
:
2462 p
+= sprintf(p
, " established");
2465 case FILTER_FLAG_OP_ANY
:
2466 case FILTER_FLAG_OP_ALL
:
2467 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2468 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2469 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2470 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2471 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2472 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2473 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2474 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2475 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2476 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2477 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2478 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2479 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2485 p
+= sprintf(p
, " fragments");
2490 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2492 static ip_filter_rulet rule
;
2493 struct in_addr addr
;
2497 if (CLI_HELP_REQUESTED
)
2501 cli_arg_help(cli
, 0,
2502 "ip", "Match IP packets",
2503 "tcp", "Match TCP packets",
2504 "udp", "Match UDP packets",
2510 // *sigh*, too darned complex
2511 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2517 cli_print(cli
, "Specify rule details");
2521 memset(&rule
, 0, sizeof(rule
));
2522 rule
.action
= (command
[0] == 'p')
2523 ? FILTER_ACTION_PERMIT
2524 : FILTER_ACTION_DENY
;
2526 if (MATCH("ip", argv
[0]))
2527 rule
.proto
= IPPROTO_IP
;
2528 else if (MATCH("udp", argv
[0]))
2529 rule
.proto
= IPPROTO_UDP
;
2530 else if (MATCH("tcp", argv
[0]))
2531 rule
.proto
= IPPROTO_TCP
;
2534 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2538 for (a
= 1, i
= 0; i
< 2; i
++)
2542 ip_filter_portt
*port
;
2547 wild
= &rule
.src_wild
;
2548 port
= &rule
.src_ports
;
2553 wild
= &rule
.dst_wild
;
2554 port
= &rule
.dst_ports
;
2557 cli_print(cli
, "Specify destination");
2562 if (MATCH("any", argv
[a
]))
2565 *wild
= INADDR_BROADCAST
;
2568 else if (MATCH("host", argv
[a
]))
2572 cli_print(cli
, "Specify host ip address");
2576 if (!inet_aton(argv
[a
], &addr
))
2578 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2590 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2594 if (!inet_aton(argv
[a
], &addr
))
2596 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2602 if (!inet_aton(argv
[++a
], &addr
))
2604 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2608 *wild
= addr
.s_addr
;
2612 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2616 if (MATCH("eq", argv
[a
]))
2617 port
->op
= FILTER_PORT_OP_EQ
;
2618 else if (MATCH("neq", argv
[a
]))
2619 port
->op
= FILTER_PORT_OP_NEQ
;
2620 else if (MATCH("gt", argv
[a
]))
2621 port
->op
= FILTER_PORT_OP_GT
;
2622 else if (MATCH("lt", argv
[a
]))
2623 port
->op
= FILTER_PORT_OP_LT
;
2624 else if (MATCH("range", argv
[a
]))
2625 port
->op
= FILTER_PORT_OP_RANGE
;
2632 cli_print(cli
, "Specify port");
2636 if (!(port
->port
= atoi(argv
[a
])))
2638 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2643 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2648 cli_print(cli
, "Specify port");
2652 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2654 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2661 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2663 if (MATCH("established", argv
[a
]))
2665 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2668 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2669 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2671 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2672 ? FILTER_FLAG_OP_ANY
2673 : FILTER_FLAG_OP_ALL
;
2677 cli_print(cli
, "Specify tcp flags");
2681 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2685 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2687 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2688 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2689 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2690 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2691 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2692 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2695 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2704 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2706 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2708 cli_print(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2718 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2725 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2727 static ip_filter_rulet rule
;
2728 struct in_addr addr
;
2730 if (CLI_HELP_REQUESTED
)
2734 cli_arg_help(cli
, argv
[0][1],
2735 "A.B.C.D", "Source address",
2736 "any", "Any source address",
2737 "host", "Source host",
2743 if (MATCH("any", argv
[0]))
2745 if (argc
== 2 && !argv
[1][1])
2746 cli_arg_help(cli
, 1, NULL
);
2748 else if (MATCH("host", argv
[0]))
2752 cli_arg_help(cli
, argv
[1][1],
2753 "A.B.C.D", "Host address",
2756 else if (argc
== 3 && !argv
[2][1])
2757 cli_arg_help(cli
, 1, NULL
);
2763 cli_arg_help(cli
, 1,
2764 "A.B.C.D", "Wildcard bits",
2767 else if (argc
== 3 && !argv
[2][1])
2768 cli_arg_help(cli
, 1, NULL
);
2776 cli_print(cli
, "Specify rule details");
2780 memset(&rule
, 0, sizeof(rule
));
2781 rule
.action
= (command
[0] == 'p')
2782 ? FILTER_ACTION_PERMIT
2783 : FILTER_ACTION_DENY
;
2785 rule
.proto
= IPPROTO_IP
;
2786 if (MATCH("any", argv
[0]))
2788 rule
.src_ip
= INADDR_ANY
;
2789 rule
.src_wild
= INADDR_BROADCAST
;
2791 else if (MATCH("host", argv
[0]))
2795 cli_print(cli
, "Specify host ip address");
2799 if (!inet_aton(argv
[1], &addr
))
2801 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2805 rule
.src_ip
= addr
.s_addr
;
2806 rule
.src_wild
= INADDR_ANY
;
2812 cli_print(cli
, "Specify source ip address and wildcard");
2816 if (!inet_aton(argv
[0], &addr
))
2818 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2822 rule
.src_ip
= addr
.s_addr
;
2826 if (!inet_aton(argv
[1], &addr
))
2828 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2832 rule
.src_wild
= addr
.s_addr
;
2835 rule
.src_wild
= INADDR_ANY
;
2841 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2844 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2845 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2846 : access_list_rule_std(cli
, command
, argv
, argc
);
2851 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2853 if (!ip_filters
[filt
].rules
[i
].action
)
2855 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2859 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2863 cli_print(cli
, "Too many rules");
2867 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2872 /* filter USER {in|out} FILTER ... */
2873 if (CLI_HELP_REQUESTED
)
2878 return cli_arg_help(cli
, 0,
2879 "USER", "Username of session to filter", NULL
);
2883 return cli_arg_help(cli
, 0,
2884 "in", "Set incoming filter",
2885 "out", "Set outgoing filter", NULL
);
2889 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2890 "NAME", "Filter name", NULL
);
2893 return cli_arg_help(cli
, argc
> 1, NULL
);
2897 if (!config
->cluster_iam_master
)
2899 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2900 fmtaddr(config
->cluster_master_address
, 0));
2905 if (argc
!= 3 && argc
!= 5)
2907 cli_print(cli
, "Specify a user and filters");
2911 if (!(s
= sessionbyuser(argv
[0])))
2913 cli_print(cli
, "User %s is not connected", argv
[0]);
2917 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2918 for (i
= 1; i
< argc
; i
+= 2)
2923 if (MATCH("in", argv
[i
]))
2925 if (session
[s
].filter_in
)
2927 cli_print(cli
, "Input already filtered");
2930 f
= &cli_session_actions
[s
].filter_in
;
2932 else if (MATCH("out", argv
[i
]))
2934 if (session
[s
].filter_out
)
2936 cli_print(cli
, "Output already filtered");
2939 f
= &cli_session_actions
[s
].filter_out
;
2943 cli_print(cli
, "Invalid filter specification");
2947 v
= find_access_list(argv
[i
+1]);
2948 if (v
< 0 || !*ip_filters
[v
].name
)
2950 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2957 cli_print(cli
, "Filtering user %s", argv
[0]);
2958 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2963 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2968 if (CLI_HELP_REQUESTED
)
2969 return cli_arg_help(cli
, argc
> 1,
2970 "USER", "Username of session to remove filters from", NULL
);
2972 if (!config
->cluster_iam_master
)
2974 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2975 fmtaddr(config
->cluster_master_address
, 0));
2982 cli_print(cli
, "Specify a user to remove filters from");
2986 for (i
= 0; i
< argc
; i
++)
2988 if (!(s
= sessionbyuser(argv
[i
])))
2990 cli_print(cli
, "User %s is not connected", argv
[i
]);
2994 if (session
[s
].filter_in
|| session
[s
].filter_out
)
2996 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
2997 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3001 cli_print(cli
, "User %s not filtered", argv
[i
]);
3008 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3012 if (CLI_HELP_REQUESTED
)
3013 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3017 cli_print(cli
, "Specify a filter name");
3021 for (i
= 0; i
< argc
; i
++)
3023 int f
= find_access_list(argv
[i
]);
3024 ip_filter_rulet
*rules
;
3026 if (f
< 0 || !*ip_filters
[f
].name
)
3028 cli_print(cli
, "Access-list %s not defined", argv
[i
]);
3035 cli_print(cli
, "%s IP access list %s",
3036 ip_filters
[f
].extended
? "Extended" : "Standard",
3037 ip_filters
[f
].name
);
3039 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3041 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3043 cli_print(cli
, "%s (%d match%s)", r
,
3044 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3046 cli_print(cli
, "%s", r
);
3053 // Convert a string in the form of abcd.ef12.3456 into char[6]
3054 void parsemac(char *string
, char mac
[6])
3056 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)
3058 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)