1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.43.2.6 2005/05/30 02:55:40 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
, "\tOpened:\t\t%u seconds", abs(time_now
- session
[s
].opened
));
410 cli_print(cli
, "\tIdle time:\t%u seconds", abs(time_now
- session
[s
].last_packet
));
411 cli_print(cli
, "\tNext Recv:\t%u", session
[s
].nr
);
412 cli_print(cli
, "\tNext Send:\t%u", session
[s
].ns
);
413 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].total_cout
, session
[s
].total_cin
);
414 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
415 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
416 cli_print(cli
, "\tRadius Session:\t%u", session
[s
].radius
);
417 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
418 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
419 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
420 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
421 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
422 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
423 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
424 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
426 cli_print(cli
, "\tIntercepted:\tno");
428 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
430 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
431 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
432 t
? "YES" : "no", t
? " (" : "",
433 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
435 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
439 b_in
= session
[s
].tbf_in
;
440 b_out
= session
[s
].tbf_out
;
442 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
443 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
444 "ByteSent", "PackSent", "PackDrop", "PackDelay");
447 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
449 (filter_list
[b_in
].next
? "*" : " "),
450 (b_in
< 100 ? "\t" : ""),
451 filter_list
[b_in
].rate
* 8,
452 filter_list
[b_in
].credit
,
453 filter_list
[b_in
].queued
,
454 filter_list
[b_in
].b_queued
,
455 filter_list
[b_in
].p_queued
,
456 filter_list
[b_in
].b_sent
,
457 filter_list
[b_in
].p_sent
,
458 filter_list
[b_in
].p_dropped
,
459 filter_list
[b_in
].p_delayed
);
462 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
464 (filter_list
[b_out
].next
? "*" : " "),
465 (b_out
< 100 ? "\t" : ""),
466 filter_list
[b_out
].rate
* 8,
467 filter_list
[b_out
].credit
,
468 filter_list
[b_out
].queued
,
469 filter_list
[b_out
].b_queued
,
470 filter_list
[b_out
].p_queued
,
471 filter_list
[b_out
].b_sent
,
472 filter_list
[b_out
].p_sent
,
473 filter_list
[b_out
].p_dropped
,
474 filter_list
[b_out
].p_delayed
);
481 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
496 for (i
= 1; i
< MAXSESSION
; i
++)
498 if (!session
[i
].opened
) continue;
499 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
502 session
[i
].user
[0] ? session
[i
].user
: "*",
503 fmtaddr(htonl(session
[i
].ip
), 0),
504 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
505 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
506 (session
[i
].walled_garden
) ? "Y" : "N",
507 abs(time_now
- (unsigned long)session
[i
].opened
),
508 (unsigned long)session
[i
].total_cout
,
509 (unsigned long)session
[i
].total_cin
,
510 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
511 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
512 session
[i
].calling
[0] ? session
[i
].calling
: "*");
517 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
519 int i
, x
, show_all
= 0;
527 if (CLI_HELP_REQUESTED
)
530 return cli_arg_help(cli
, 1,
531 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
534 return cli_arg_help(cli
, 1,
535 "all", "Show all tunnels, including unused",
536 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
543 if (strcmp(argv
[0], "all") == 0)
549 // Show individual tunnel
550 for (i
= 0; i
< argc
; i
++)
555 if (t
<= 0 || t
>= MAXTUNNEL
)
557 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
560 cli_print(cli
, "\r\nTunnel %d:", t
);
561 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
562 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
563 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
564 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
565 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
566 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
567 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
568 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
569 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
571 for (x
= 0; x
< MAXSESSION
; x
++)
572 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
573 sprintf(s
, "%s%u ", s
, x
);
575 cli_print(cli
, "\tSessions:\t%s", s
);
581 // Show tunnel summary
582 cli_print(cli
, "%4s %20s %20s %6s %s",
589 for (i
= 1; i
< MAXTUNNEL
; i
++)
592 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
594 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
595 cli_print(cli
, "%4d %20s %20s %6s %6d",
597 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
598 fmtaddr(htonl(tunnel
[i
].ip
), 0),
599 states
[tunnel
[i
].state
],
606 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
613 if (CLI_HELP_REQUESTED
)
614 return cli_arg_help(cli
, 1,
615 "USER", "Show details for specific username",
618 for (i
= 0; i
< MAXSESSION
; i
++)
620 if (!session
[i
].opened
) continue;
621 if (!session
[i
].user
[0]) continue;
625 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
627 if (strcmp(argv
[j
], session
[i
].user
) == 0)
629 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
630 sargv
[sargc
] = sid
[sargc
];
638 cli_print(cli
, "%s", session
[i
].user
);
642 return cmd_show_session(cli
, "users", sargv
, sargc
);
647 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
649 if (CLI_HELP_REQUESTED
)
650 return CLI_HELP_NO_ARGS
;
652 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
653 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
654 GET_STAT(tun_rx_bytes
),
655 GET_STAT(tun_rx_packets
),
656 GET_STAT(tun_rx_errors
),
657 GET_STAT(tun_rx_dropped
));
658 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
659 GET_STAT(tun_tx_bytes
),
660 GET_STAT(tun_tx_packets
),
661 GET_STAT(tun_tx_errors
));
664 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
665 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
666 GET_STAT(tunnel_rx_bytes
),
667 GET_STAT(tunnel_rx_packets
),
668 GET_STAT(tunnel_rx_errors
));
669 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
670 GET_STAT(tunnel_tx_bytes
),
671 GET_STAT(tunnel_tx_packets
),
672 GET_STAT(tunnel_tx_errors
),
673 GET_STAT(tunnel_retries
));
676 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
677 cli_print(cli
, "-----------------------------------------");
678 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
679 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
680 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
681 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
682 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
683 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
684 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
685 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
686 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
687 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
688 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
689 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
690 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
691 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
692 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
693 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
694 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
695 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
699 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
700 cli_print(cli
, "-----------------------------------------");
701 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
702 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
703 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
704 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
705 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
706 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
707 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
708 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
709 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
710 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
711 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
712 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
713 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
714 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
715 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
716 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
717 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
718 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
719 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
720 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
721 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
722 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
723 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
724 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
725 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
726 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
730 time_t l
= GET_STAT(last_reset
);
732 char *p
= strchr(t
, '\n');
736 cli_print(cli
, "Last counter reset %s", t
);
742 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
748 if (CLI_HELP_REQUESTED
)
749 return cli_arg_help(cli
, 1,
750 "tag", "Include CVS release tag",
751 "file", "Include file versions",
754 for (i
= 0; i
< argc
; i
++)
755 if (!strcmp(argv
[i
], "tag"))
757 else if (!strcmp(argv
[i
], "file"))
760 cli_print(cli
, "L2TPNS %s", VERSION
);
763 char const *p
= strchr(cvs_name
, ':');
775 e
= strpbrk(p
, " \t$");
776 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
781 extern linked_list
*loaded_plugins
;
784 cli_print(cli
, "Files:");
785 cli_print(cli
, " %s", cvs_id_arp
);
787 cli_print(cli
, " %s", cvs_id_bgp
);
789 cli_print(cli
, " %s", cvs_id_cli
);
790 cli_print(cli
, " %s", cvs_id_cluster
);
791 cli_print(cli
, " %s", cvs_id_constants
);
792 cli_print(cli
, " %s", cvs_id_control
);
793 cli_print(cli
, " %s", cvs_id_icmp
);
794 cli_print(cli
, " %s", cvs_id_l2tpns
);
795 cli_print(cli
, " %s", cvs_id_ll
);
796 cli_print(cli
, " %s", cvs_id_md5
);
797 cli_print(cli
, " %s", cvs_id_ppp
);
798 cli_print(cli
, " %s", cvs_id_radius
);
799 cli_print(cli
, " %s", cvs_id_tbf
);
800 cli_print(cli
, " %s", cvs_id_util
);
802 ll_reset(loaded_plugins
);
803 while ((p
= ll_next(loaded_plugins
)))
805 char const **id
= dlsym(p
, "cvs_id");
807 cli_print(cli
, " %s", *id
);
814 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
817 int used
= 0, free
= 0, show_all
= 0;
819 if (!config
->cluster_iam_master
)
821 cli_print(cli
, "Can't do this on a slave. Do it on %s",
822 fmtaddr(config
->cluster_master_address
, 0));
827 if (CLI_HELP_REQUESTED
)
830 return cli_arg_help(cli
, 1, NULL
);
832 return cli_arg_help(cli
, 1,
833 "all", "Show all pool addresses, including unused",
837 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
841 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
842 for (i
= 0; i
< MAXIPPOOL
; i
++)
844 if (!ip_address_pool
[i
].address
) continue;
845 if (ip_address_pool
[i
].assigned
)
847 cli_print(cli
, "%-15s\tY %8d %s",
848 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
849 ip_address_pool
[i
].session
,
850 session
[ip_address_pool
[i
].session
].user
);
856 if (ip_address_pool
[i
].last
)
857 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
858 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
859 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
862 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
869 cli_print(cli
, "(Not displaying unused addresses)");
871 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
875 static FILE *save_config_fh
= 0;
876 static void print_save_config(struct cli_def
*cli
, char *string
)
879 fprintf(save_config_fh
, "%s\n", string
);
882 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
884 if (CLI_HELP_REQUESTED
)
885 return CLI_HELP_NO_ARGS
;
887 if ((save_config_fh
= fopen(config
->config_file
, "w")))
889 cli_print(cli
, "Writing configuration");
890 cli_print_callback(cli
, print_save_config
);
891 cmd_show_run(cli
, command
, argv
, argc
);
892 cli_print_callback(cli
, NULL
);
893 fclose(save_config_fh
);
898 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
903 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
905 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
909 if (CLI_HELP_REQUESTED
)
910 return CLI_HELP_NO_ARGS
;
912 cli_print(cli
, "# Current configuration:");
914 for (i
= 0; config_values
[i
].key
; i
++)
916 void *value
= ((void *)config
) + config_values
[i
].offset
;
917 if (config_values
[i
].type
== STRING
)
918 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
919 else if (config_values
[i
].type
== IP
)
920 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(unsigned *)value
, 0));
921 else if (config_values
[i
].type
== SHORT
)
922 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
923 else if (config_values
[i
].type
== BOOL
)
924 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
925 else if (config_values
[i
].type
== INT
)
926 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
927 else if (config_values
[i
].type
== UNSIGNED_LONG
)
928 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
929 else if (config_values
[i
].type
== MAC
)
930 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
931 *(unsigned short *)(value
+ 0),
932 *(unsigned short *)(value
+ 1),
933 *(unsigned short *)(value
+ 2),
934 *(unsigned short *)(value
+ 3),
935 *(unsigned short *)(value
+ 4),
936 *(unsigned short *)(value
+ 5));
939 cli_print(cli
, "# Plugins");
940 for (i
= 0; i
< MAXPLUGINS
; i
++)
942 if (*config
->plugins
[i
])
944 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
949 if (config
->as_number
)
954 cli_print(cli
, "# BGP");
955 cli_print(cli
, "router bgp %u", config
->as_number
);
956 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
958 if (!config
->neighbour
[i
].name
[0])
961 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
963 k
= config
->neighbour
[i
].keepalive
;
964 h
= config
->neighbour
[i
].hold
;
971 k
= BGP_KEEPALIVE_TIME
;
977 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
982 cli_print(cli
, "# Filters");
983 for (i
= 0; i
< MAXFILTER
; i
++)
985 ip_filter_rulet
*rules
;
986 if (!*ip_filters
[i
].name
)
989 cli_print(cli
, "ip access-list %s %s",
990 ip_filters
[i
].extended
? "extended" : "standard",
993 rules
= ip_filters
[i
].rules
;
994 while (rules
->action
)
995 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
998 cli_print(cli
, "# end");
1002 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1004 int i
, free
= 0, used
= 0, show_all
= 0;
1015 if (CLI_HELP_REQUESTED
)
1018 return cli_arg_help(cli
, 1, NULL
);
1020 return cli_arg_help(cli
, 1,
1021 "all", "Show all RADIUS sessions, including unused",
1025 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1029 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1032 for (i
= 1; i
< MAXRADIUS
; i
++)
1034 if (radius
[i
].state
== RADIUSNULL
)
1039 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1041 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1045 states
[radius
[i
].state
],
1051 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1056 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1060 if (CLI_HELP_REQUESTED
)
1061 return CLI_HELP_NO_ARGS
;
1063 cli_print(cli
, "Plugins currently loaded:");
1064 for (i
= 0; i
< MAXPLUGINS
; i
++)
1065 if (*config
->plugins
[i
])
1066 cli_print(cli
, " %s", config
->plugins
[i
]);
1071 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1075 if (CLI_HELP_REQUESTED
)
1076 return CLI_HELP_NO_ARGS
;
1078 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1087 for (i
= 0; i
< MAXSESSION
; i
++)
1089 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1090 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1094 session
[i
].throttle_in
,
1095 session
[i
].throttle_out
,
1097 session
[i
].tbf_out
);
1103 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1105 if (CLI_HELP_REQUESTED
)
1106 return CLI_HELP_NO_ARGS
;
1108 cli_print(cli
, " _\n"
1114 " ( \\ `. `-. _,.-:\\\n"
1115 " \\ \\ `. `-._ __..--' ,-';/\n"
1116 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1117 " `. `. `-._ __..--' ,' /\n"
1118 " `. `-_ ``--..'' _.-' ,'\n"
1119 " `-_ `-.___ __,--' ,'\n"
1120 " `-.__ `----\"\"\" __.-'\n"
1121 "hh `--..____..--'");
1126 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1128 if (CLI_HELP_REQUESTED
)
1129 return CLI_HELP_NO_ARGS
;
1131 memset(_statistics
, 0, sizeof(struct Tstats
));
1132 SET_STAT(last_reset
, time(NULL
));
1134 cli_print(cli
, "Counters cleared");
1138 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1143 if (CLI_HELP_REQUESTED
)
1144 return cli_arg_help(cli
, argc
> 1,
1145 "USER", "Username of session to drop", NULL
);
1147 if (!config
->cluster_iam_master
)
1149 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1150 fmtaddr(config
->cluster_master_address
, 0));
1157 cli_print(cli
, "Specify a user to drop");
1161 for (i
= 0; i
< argc
; i
++)
1163 if (!(s
= sessionbyuser(argv
[i
])))
1165 cli_print(cli
, "User %s is not connected", argv
[i
]);
1169 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1171 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1172 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1179 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1184 if (CLI_HELP_REQUESTED
)
1185 return cli_arg_help(cli
, argc
> 1,
1186 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1188 if (!config
->cluster_iam_master
)
1190 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1191 fmtaddr(config
->cluster_master_address
, 0));
1198 cli_print(cli
, "Specify a tunnel to drop");
1202 for (i
= 0; i
< argc
; i
++)
1204 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1206 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1212 cli_print(cli
, "Tunnel %d is not connected", t
);
1218 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1222 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1223 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1229 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1234 if (CLI_HELP_REQUESTED
)
1235 return cli_arg_help(cli
, argc
> 1,
1236 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1238 if (!config
->cluster_iam_master
)
1240 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1241 fmtaddr(config
->cluster_master_address
, 0));
1248 cli_print(cli
, "Specify a session id to drop");
1252 for (i
= 0; i
< argc
; i
++)
1254 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1256 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1260 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1262 cli_print(cli
, "Dropping session %d", s
);
1263 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1267 cli_print(cli
, "Session %d is not active.", s
);
1274 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1280 if (CLI_HELP_REQUESTED
)
1285 return cli_arg_help(cli
, 0,
1286 "USER", "Username of session to snoop", NULL
);
1289 return cli_arg_help(cli
, 0,
1290 "A.B.C.D", "IP address of snoop destination", NULL
);
1293 return cli_arg_help(cli
, 0,
1294 "N", "Port of snoop destination", NULL
);
1298 return cli_arg_help(cli
, 1, NULL
);
1305 if (!config
->cluster_iam_master
)
1307 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1308 fmtaddr(config
->cluster_master_address
, 0));
1315 cli_print(cli
, "Specify username, ip and port");
1319 if (!(s
= sessionbyuser(argv
[0])))
1321 cli_print(cli
, "User %s is not connected", argv
[0]);
1325 ip
= inet_addr(argv
[1]);
1326 if (!ip
|| ip
== INADDR_NONE
)
1328 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1332 port
= atoi(argv
[2]);
1335 cli_print(cli
, "Invalid port %s", argv
[2]);
1339 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1340 cli_session_actions
[s
].snoop_ip
= ip
;
1341 cli_session_actions
[s
].snoop_port
= port
;
1342 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1347 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1352 if (CLI_HELP_REQUESTED
)
1353 return cli_arg_help(cli
, argc
> 1,
1354 "USER", "Username of session to unsnoop", NULL
);
1356 if (!config
->cluster_iam_master
)
1358 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1359 fmtaddr(config
->cluster_master_address
, 0));
1366 cli_print(cli
, "Specify a user to unsnoop");
1370 for (i
= 0; i
< argc
; i
++)
1372 if (!(s
= sessionbyuser(argv
[i
])))
1374 cli_print(cli
, "User %s is not connected", argv
[i
]);
1378 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1379 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1385 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1392 throttle USER - throttle in/out to default rate
1393 throttle USER RATE - throttle in/out to default rate
1394 throttle USER in RATE - throttle input only
1395 throttle USER out RATE - throttle output only
1396 throttle USER in RATE out RATE - throttle both
1399 if (CLI_HELP_REQUESTED
)
1404 return cli_arg_help(cli
, 0,
1405 "USER", "Username of session to throttle", NULL
);
1408 return cli_arg_help(cli
, 1,
1409 "RATE", "Rate in kbps (in and out)",
1410 "in", "Select incoming rate",
1411 "out", "Select outgoing rate", NULL
);
1414 return cli_arg_help(cli
, 1,
1415 "in", "Select incoming rate",
1416 "out", "Select outgoing rate", NULL
);
1419 if (isdigit(argv
[1][0]))
1420 return cli_arg_help(cli
, 1, NULL
);
1423 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1426 return cli_arg_help(cli
, argc
> 1, NULL
);
1430 if (!config
->cluster_iam_master
)
1432 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1433 fmtaddr(config
->cluster_master_address
, 0));
1440 cli_print(cli
, "Specify a user to throttle");
1444 if (!(s
= sessionbyuser(argv
[0])))
1446 cli_print(cli
, "User %s is not connected", argv
[0]);
1452 rate_in
= rate_out
= config
->rl_rate
;
1456 rate_in
= rate_out
= atoi(argv
[1]);
1459 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1463 else if (argc
== 3 || argc
== 5)
1466 for (i
= 1; i
< argc
- 1; i
+= 2)
1469 if (MATCH("in", argv
[i
]))
1470 r
= rate_in
= atoi(argv
[i
+1]);
1471 else if (MATCH("out", argv
[i
]))
1472 r
= rate_out
= atoi(argv
[i
+1]);
1476 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1483 cli_print(cli
, "Invalid arguments");
1487 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1489 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1493 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1494 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1495 cli_session_actions
[s
].throttle_in
= rate_in
;
1497 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1498 cli_session_actions
[s
].throttle_out
= rate_out
;
1500 if (cli_session_actions
[s
].throttle_in
== -1 &&
1501 cli_session_actions
[s
].throttle_out
== -1)
1503 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1507 cli_print(cli
, "Throttling user %s", argv
[0]);
1508 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1513 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1518 if (CLI_HELP_REQUESTED
)
1519 return cli_arg_help(cli
, argc
> 1,
1520 "USER", "Username of session to unthrottle", NULL
);
1522 if (!config
->cluster_iam_master
)
1524 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1525 fmtaddr(config
->cluster_master_address
, 0));
1532 cli_print(cli
, "Specify a user to unthrottle");
1536 for (i
= 0; i
< argc
; i
++)
1538 if (!(s
= sessionbyuser(argv
[i
])))
1540 cli_print(cli
, "User %s is not connected", argv
[i
]);
1544 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1546 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1547 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1551 cli_print(cli
, "User %s not throttled", argv
[i
]);
1558 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1562 if (CLI_HELP_REQUESTED
)
1563 return cli_arg_help(cli
, 1,
1564 "all", "Enable debugging for all except \"data\"",
1565 "critical", "", // FIXME: add descriptions
1575 char *p
= (char *) &debug_flags
;
1576 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1580 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1581 (debug_flags
.critical
) ? " critical" : "",
1582 (debug_flags
.error
) ? " error" : "",
1583 (debug_flags
.warning
) ? " warning" : "",
1584 (debug_flags
.info
) ? " info" : "",
1585 (debug_flags
.calls
) ? " calls" : "",
1586 (debug_flags
.data
) ? " data" : "");
1592 cli_print(cli
, "Debugging off");
1596 for (i
= 0; i
< argc
; i
++)
1598 int len
= strlen(argv
[i
]);
1600 if (argv
[i
][0] == 'c' && len
< 2)
1601 len
= 2; /* distinguish [cr]itical from [ca]lls */
1603 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1604 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1605 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1606 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1607 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1608 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1609 if (!strncmp("all", argv
[i
], len
))
1611 memset(&debug_flags
, 1, sizeof(debug_flags
));
1612 debug_flags
.data
= 0;
1616 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1622 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1626 if (CLI_HELP_REQUESTED
)
1627 return cli_arg_help(cli
, 1,
1628 "all", "Disable all debugging",
1629 "critical", "", // FIXME: add descriptions
1639 memset(&debug_flags
, 0, sizeof(debug_flags
));
1643 for (i
= 0; i
< argc
; i
++)
1645 int len
= strlen(argv
[i
]);
1647 if (argv
[i
][0] == 'c' && len
< 2)
1648 len
= 2; /* distinguish [cr]itical from [ca]lls */
1650 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1651 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1652 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1653 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1654 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1655 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1656 if (!strncmp("all", argv
[i
], len
))
1658 memset(&debug_flags
, 0, sizeof(debug_flags
));
1662 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1668 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1670 int i
, firstfree
= 0;
1672 if (CLI_HELP_REQUESTED
)
1673 return cli_arg_help(cli
, argc
> 1,
1674 "PLUGIN", "Name of plugin to load", NULL
);
1678 cli_print(cli
, "Specify a plugin to load");
1682 for (i
= 0; i
< MAXPLUGINS
; i
++)
1684 if (!*config
->plugins
[i
] && !firstfree
)
1686 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1688 cli_print(cli
, "Plugin is already loaded");
1695 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1696 config
->reload_config
= 1;
1697 cli_print(cli
, "Loading plugin %s", argv
[0]);
1703 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1707 if (CLI_HELP_REQUESTED
)
1708 return cli_arg_help(cli
, argc
> 1,
1709 "PLUGIN", "Name of plugin to unload", NULL
);
1713 cli_print(cli
, "Specify a plugin to remove");
1717 for (i
= 0; i
< MAXPLUGINS
; i
++)
1719 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1721 config
->reload_config
= 1;
1722 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1727 cli_print(cli
, "Plugin is not loaded");
1731 static char *duration(time_t secs
)
1733 static char *buf
= NULL
;
1736 if (!buf
) buf
= calloc(64, 1);
1740 int days
= secs
/ 86400;
1741 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1747 int mins
= secs
/ 60;
1748 int hrs
= mins
/ 60;
1751 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1753 else if (secs
>= 60)
1755 int mins
= secs
/ 60;
1756 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1759 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1764 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1767 char buf
[100], *p
= buf
, *loads
[3];
1768 int i
, num_sessions
= 0;
1770 if (CLI_HELP_REQUESTED
)
1771 return CLI_HELP_NO_ARGS
;
1773 fh
= fopen("/proc/loadavg", "r");
1774 fgets(buf
, 100, fh
);
1777 for (i
= 0; i
< 3; i
++)
1778 loads
[i
] = strdup(strsep(&p
, " "));
1781 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1783 for (i
= 1; i
< MAXSESSION
; i
++)
1784 if (session
[i
].opened
) num_sessions
++;
1786 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1788 duration(time_now
- config
->start_time
),
1790 loads
[0], loads
[1], loads
[2]
1792 for (i
= 0; i
< 3; i
++)
1793 if (loads
[i
]) free(loads
[i
]);
1795 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1800 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1804 if (CLI_HELP_REQUESTED
)
1810 int len
= strlen(argv
[0])-1;
1811 for (i
= 0; config_values
[i
].key
; i
++)
1812 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1813 cli_print(cli
, " %s", config_values
[i
].key
);
1819 return cli_arg_help(cli
, 0,
1820 "VALUE", "Value for variable", NULL
);
1824 return cli_arg_help(cli
, 1, NULL
);
1833 cli_print(cli
, "Specify variable and value");
1837 for (i
= 0; config_values
[i
].key
; i
++)
1839 void *value
= ((void *)config
) + config_values
[i
].offset
;
1840 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1842 // Found a value to set
1843 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1844 switch (config_values
[i
].type
)
1847 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1850 *(int *)value
= atoi(argv
[1]);
1853 *(unsigned long *)value
= atol(argv
[1]);
1856 *(short *)value
= atoi(argv
[1]);
1859 *(unsigned *)value
= inet_addr(argv
[1]);
1862 parsemac(argv
[1], (char *)value
);
1865 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1871 cli_print(cli
, "Unknown variable type");
1874 config
->reload_config
= 1;
1879 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1883 int regular_stuff(struct cli_def
*cli
)
1889 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1891 char *m
= ringbuffer
->buffer
[i
].message
;
1897 switch (ringbuffer
->buffer
[i
].level
)
1899 case 0: show
= debug_flags
.critical
; break;
1900 case 1: show
= debug_flags
.error
; break;
1901 case 2: show
= debug_flags
.warning
; break;
1902 case 3: show
= debug_flags
.info
; break;
1903 case 4: show
= debug_flags
.calls
; break;
1904 case 5: show
= debug_flags
.data
; break;
1907 if (!show
) continue;
1909 if (!(p
= strchr(m
, '\n')))
1912 cli_print(cli
, "\r%s-%u-%u %.*s",
1913 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1914 ringbuffer
->buffer
[i
].tunnel
,
1915 ringbuffer
->buffer
[i
].session
,
1921 debug_rb_tail
= ringbuffer
->tail
;
1929 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1933 if (CLI_HELP_REQUESTED
)
1934 return cli_arg_help(cli
, argc
> 1,
1935 "<1-65535>", "Autonomous system number", NULL
);
1937 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1939 cli_print(cli
, "Invalid autonomous system number");
1943 if (bgp_configured
&& as
!= config
->as_number
)
1945 cli_print(cli
, "Can't change local AS on a running system");
1949 config
->as_number
= as
;
1950 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1955 static int find_bgp_neighbour(char const *name
)
1960 in_addr_t addrs
[4] = { 0 };
1963 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1966 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1967 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1969 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1971 if (!config
->neighbour
[i
].name
[0])
1973 if (new == -1) new = i
;
1977 if (!strcmp(name
, config
->neighbour
[i
].name
))
1980 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1983 for (a
= h
->h_addr_list
; *a
; a
++)
1986 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1987 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1995 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2001 if (CLI_HELP_REQUESTED
)
2006 return cli_arg_help(cli
, 0,
2007 "A.B.C.D", "BGP neighbour address",
2008 "NAME", "BGP neighbour name",
2012 return cli_arg_help(cli
, 0,
2013 "remote-as", "Set remote autonomous system number",
2014 "timers", "Set timers",
2018 if (MATCH("remote-as", argv
[1]))
2019 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2021 if (MATCH("timers", argv
[1]))
2024 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2027 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2029 if (argc
== 5 && !argv
[4][1])
2030 return cli_arg_help(cli
, 1, NULL
);
2039 cli_print(cli
, "Invalid arguments");
2043 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2045 cli_print(cli
, "Invalid neighbour");
2051 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2055 if (MATCH("remote-as", argv
[1]))
2057 int as
= atoi(argv
[2]);
2058 if (as
< 0 || as
> 65535)
2060 cli_print(cli
, "Invalid autonomous system number");
2064 if (!config
->neighbour
[i
].name
[0])
2066 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2067 config
->neighbour
[i
].keepalive
= -1;
2068 config
->neighbour
[i
].hold
= -1;
2071 config
->neighbour
[i
].as
= as
;
2075 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2077 cli_print(cli
, "Invalid arguments");
2081 if (!config
->neighbour
[i
].name
[0])
2083 cli_print(cli
, "Specify remote-as first");
2087 keepalive
= atoi(argv
[2]);
2088 hold
= atoi(argv
[3]);
2090 if (keepalive
< 1 || keepalive
> 65535)
2092 cli_print(cli
, "Invalid keepalive time");
2096 if (hold
< 3 || hold
> 65535)
2098 cli_print(cli
, "Invalid hold time");
2102 if (keepalive
== BGP_KEEPALIVE_TIME
)
2103 keepalive
= -1; // using default value
2105 if (hold
== BGP_HOLD_TIME
)
2108 config
->neighbour
[i
].keepalive
= keepalive
;
2109 config
->neighbour
[i
].hold
= hold
;
2114 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2118 if (CLI_HELP_REQUESTED
)
2119 return cli_arg_help(cli
, argc
> 0,
2120 "A.B.C.D", "BGP neighbour address",
2121 "NAME", "BGP neighbour name",
2126 cli_print(cli
, "Specify a BGP neighbour");
2130 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2132 cli_print(cli
, "Invalid neighbour");
2136 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2138 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2142 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2146 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2152 if (!bgp_configured
)
2155 if (CLI_HELP_REQUESTED
)
2156 return cli_arg_help(cli
, 1,
2157 "A.B.C.D", "BGP neighbour address",
2158 "NAME", "BGP neighbour name",
2161 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2162 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2166 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2168 if (!*bgp_peers
[i
].name
)
2171 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2172 if (argc
&& strcmp(addr
, argv
[0]) &&
2173 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2179 cli_print(cli
, "Peer AS Address "
2180 "State Retries Retry in Route Pend Timers");
2181 cli_print(cli
, "------------------ ----- --------------- "
2182 "----------- ------- -------- ----- ---- ---------");
2185 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2189 bgp_state_str(bgp_peers
[i
].state
),
2190 bgp_peers
[i
].retry_count
,
2191 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2192 bgp_peers
[i
].routing
? "yes" : "no",
2193 bgp_peers
[i
].update_routes
? "yes" : "no",
2194 bgp_peers
[i
].keepalive
,
2201 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2206 if (!bgp_configured
)
2209 if (CLI_HELP_REQUESTED
)
2210 return cli_arg_help(cli
, 1,
2211 "A.B.C.D", "BGP neighbour address",
2212 "NAME", "BGP neighbour name",
2215 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2217 if (bgp_peers
[i
].state
!= Established
)
2220 if (!bgp_peers
[i
].routing
)
2223 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2224 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2227 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2228 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2234 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2239 if (!bgp_configured
)
2242 if (CLI_HELP_REQUESTED
)
2243 return cli_arg_help(cli
, 1,
2244 "A.B.C.D", "BGP neighbour address",
2245 "NAME", "BGP neighbour name",
2248 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2250 if (bgp_peers
[i
].state
!= Established
)
2253 if (bgp_peers
[i
].routing
)
2256 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2257 if (argc
&& strcmp(addr
, argv
[0]) &&
2258 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2261 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2262 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2268 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2273 if (!bgp_configured
)
2276 if (CLI_HELP_REQUESTED
)
2277 return cli_arg_help(cli
, 1,
2278 "A.B.C.D", "BGP neighbour address",
2279 "NAME", "BGP neighbour name",
2282 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2284 if (!*bgp_peers
[i
].name
)
2287 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2288 if (argc
&& strcmp(addr
, argv
[0]) &&
2289 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2292 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2293 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2301 static int find_access_list(char const *name
)
2305 for (i
= 0; i
< MAXFILTER
; i
++)
2306 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2312 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2316 if (CLI_HELP_REQUESTED
)
2321 return cli_arg_help(cli
, 0,
2322 "standard", "Standard syntax",
2323 "extended", "Extended syntax",
2327 return cli_arg_help(cli
, argv
[1][1],
2328 "NAME", "Access-list name",
2332 if (argc
== 3 && !argv
[2][1])
2333 return cli_arg_help(cli
, 1, NULL
);
2341 cli_print(cli
, "Specify access-list type and name");
2345 if (MATCH("standard", argv
[0]))
2347 else if (MATCH("extended", argv
[0]))
2351 cli_print(cli
, "Invalid access-list type");
2355 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2356 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2358 cli_print(cli
, "Invalid access-list name");
2362 filt
= find_access_list(argv
[1]);
2367 cli_print(cli
, "Too many access-lists");
2372 if (!*ip_filters
[filt
].name
)
2374 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2375 strcpy(ip_filters
[filt
].name
, argv
[1]);
2376 ip_filters
[filt
].extended
= extended
;
2378 else if (ip_filters
[filt
].extended
!= extended
)
2380 cli_print(cli
, "Access-list is %s",
2381 ip_filters
[filt
].extended
? "extended" : "standard");
2386 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2390 if (filt
< 0 || !*ip_filters
[filt
].name
)
2392 cli_print(cli
, "Access-list not defined");
2397 if (ip_filters
[filt
].used
)
2399 cli_print(cli
, "Access-list in use");
2403 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2407 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2409 return access_list(cli
, argv
, argc
, 1);
2412 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2414 return access_list(cli
, argv
, argc
, 0);
2417 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2419 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2420 return sprintf(buf
, " any");
2422 if (wild
== INADDR_ANY
)
2423 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2425 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2428 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2432 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2433 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2434 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2435 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2436 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2442 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2444 static char buf
[256];
2447 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2450 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2451 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2454 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2458 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2459 p
+= show_ports(p
, &rule
->src_ports
);
2461 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2462 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2463 p
+= show_ports(p
, &rule
->dst_ports
);
2465 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2467 switch (rule
->tcp_flag_op
)
2469 case FILTER_FLAG_OP_EST
:
2470 p
+= sprintf(p
, " established");
2473 case FILTER_FLAG_OP_ANY
:
2474 case FILTER_FLAG_OP_ALL
:
2475 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2476 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2477 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2478 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2479 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2480 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2481 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2482 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2483 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2484 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2485 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2486 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2487 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2493 p
+= sprintf(p
, " fragments");
2498 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2500 static ip_filter_rulet rule
;
2501 struct in_addr addr
;
2505 if (CLI_HELP_REQUESTED
)
2509 cli_arg_help(cli
, 0,
2510 "ip", "Match IP packets",
2511 "tcp", "Match TCP packets",
2512 "udp", "Match UDP packets",
2518 // *sigh*, too darned complex
2519 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2525 cli_print(cli
, "Specify rule details");
2529 memset(&rule
, 0, sizeof(rule
));
2530 rule
.action
= (command
[0] == 'p')
2531 ? FILTER_ACTION_PERMIT
2532 : FILTER_ACTION_DENY
;
2534 if (MATCH("ip", argv
[0]))
2535 rule
.proto
= IPPROTO_IP
;
2536 else if (MATCH("udp", argv
[0]))
2537 rule
.proto
= IPPROTO_UDP
;
2538 else if (MATCH("tcp", argv
[0]))
2539 rule
.proto
= IPPROTO_TCP
;
2542 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2546 for (a
= 1, i
= 0; i
< 2; i
++)
2550 ip_filter_portt
*port
;
2555 wild
= &rule
.src_wild
;
2556 port
= &rule
.src_ports
;
2561 wild
= &rule
.dst_wild
;
2562 port
= &rule
.dst_ports
;
2565 cli_print(cli
, "Specify destination");
2570 if (MATCH("any", argv
[a
]))
2573 *wild
= INADDR_BROADCAST
;
2576 else if (MATCH("host", argv
[a
]))
2580 cli_print(cli
, "Specify host ip address");
2584 if (!inet_aton(argv
[a
], &addr
))
2586 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2598 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2602 if (!inet_aton(argv
[a
], &addr
))
2604 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2610 if (!inet_aton(argv
[++a
], &addr
))
2612 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2616 *wild
= addr
.s_addr
;
2620 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2624 if (MATCH("eq", argv
[a
]))
2625 port
->op
= FILTER_PORT_OP_EQ
;
2626 else if (MATCH("neq", argv
[a
]))
2627 port
->op
= FILTER_PORT_OP_NEQ
;
2628 else if (MATCH("gt", argv
[a
]))
2629 port
->op
= FILTER_PORT_OP_GT
;
2630 else if (MATCH("lt", argv
[a
]))
2631 port
->op
= FILTER_PORT_OP_LT
;
2632 else if (MATCH("range", argv
[a
]))
2633 port
->op
= FILTER_PORT_OP_RANGE
;
2640 cli_print(cli
, "Specify port");
2644 if (!(port
->port
= atoi(argv
[a
])))
2646 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2651 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2656 cli_print(cli
, "Specify port");
2660 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2662 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2669 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2671 if (MATCH("established", argv
[a
]))
2673 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2676 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2677 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2679 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2680 ? FILTER_FLAG_OP_ANY
2681 : FILTER_FLAG_OP_ALL
;
2685 cli_print(cli
, "Specify tcp flags");
2689 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2693 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2695 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2696 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2697 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2698 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2699 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2700 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2703 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2712 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2714 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2716 cli_print(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2726 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2733 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2735 static ip_filter_rulet rule
;
2736 struct in_addr addr
;
2738 if (CLI_HELP_REQUESTED
)
2742 cli_arg_help(cli
, argv
[0][1],
2743 "A.B.C.D", "Source address",
2744 "any", "Any source address",
2745 "host", "Source host",
2751 if (MATCH("any", argv
[0]))
2753 if (argc
== 2 && !argv
[1][1])
2754 cli_arg_help(cli
, 1, NULL
);
2756 else if (MATCH("host", argv
[0]))
2760 cli_arg_help(cli
, argv
[1][1],
2761 "A.B.C.D", "Host address",
2764 else if (argc
== 3 && !argv
[2][1])
2765 cli_arg_help(cli
, 1, NULL
);
2771 cli_arg_help(cli
, 1,
2772 "A.B.C.D", "Wildcard bits",
2775 else if (argc
== 3 && !argv
[2][1])
2776 cli_arg_help(cli
, 1, NULL
);
2784 cli_print(cli
, "Specify rule details");
2788 memset(&rule
, 0, sizeof(rule
));
2789 rule
.action
= (command
[0] == 'p')
2790 ? FILTER_ACTION_PERMIT
2791 : FILTER_ACTION_DENY
;
2793 rule
.proto
= IPPROTO_IP
;
2794 if (MATCH("any", argv
[0]))
2796 rule
.src_ip
= INADDR_ANY
;
2797 rule
.src_wild
= INADDR_BROADCAST
;
2799 else if (MATCH("host", argv
[0]))
2803 cli_print(cli
, "Specify host ip address");
2807 if (!inet_aton(argv
[1], &addr
))
2809 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2813 rule
.src_ip
= addr
.s_addr
;
2814 rule
.src_wild
= INADDR_ANY
;
2820 cli_print(cli
, "Specify source ip address and wildcard");
2824 if (!inet_aton(argv
[0], &addr
))
2826 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2830 rule
.src_ip
= addr
.s_addr
;
2834 if (!inet_aton(argv
[1], &addr
))
2836 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2840 rule
.src_wild
= addr
.s_addr
;
2843 rule
.src_wild
= INADDR_ANY
;
2849 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2852 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2853 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2854 : access_list_rule_std(cli
, command
, argv
, argc
);
2859 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2861 if (!ip_filters
[filt
].rules
[i
].action
)
2863 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2867 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2871 cli_print(cli
, "Too many rules");
2875 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2880 /* filter USER {in|out} FILTER ... */
2881 if (CLI_HELP_REQUESTED
)
2886 return cli_arg_help(cli
, 0,
2887 "USER", "Username of session to filter", NULL
);
2891 return cli_arg_help(cli
, 0,
2892 "in", "Set incoming filter",
2893 "out", "Set outgoing filter", NULL
);
2897 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2898 "NAME", "Filter name", NULL
);
2901 return cli_arg_help(cli
, argc
> 1, NULL
);
2905 if (!config
->cluster_iam_master
)
2907 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2908 fmtaddr(config
->cluster_master_address
, 0));
2913 if (argc
!= 3 && argc
!= 5)
2915 cli_print(cli
, "Specify a user and filters");
2919 if (!(s
= sessionbyuser(argv
[0])))
2921 cli_print(cli
, "User %s is not connected", argv
[0]);
2925 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2926 for (i
= 1; i
< argc
; i
+= 2)
2931 if (MATCH("in", argv
[i
]))
2933 if (session
[s
].filter_in
)
2935 cli_print(cli
, "Input already filtered");
2938 f
= &cli_session_actions
[s
].filter_in
;
2940 else if (MATCH("out", argv
[i
]))
2942 if (session
[s
].filter_out
)
2944 cli_print(cli
, "Output already filtered");
2947 f
= &cli_session_actions
[s
].filter_out
;
2951 cli_print(cli
, "Invalid filter specification");
2955 v
= find_access_list(argv
[i
+1]);
2956 if (v
< 0 || !*ip_filters
[v
].name
)
2958 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2965 cli_print(cli
, "Filtering user %s", argv
[0]);
2966 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2971 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2976 if (CLI_HELP_REQUESTED
)
2977 return cli_arg_help(cli
, argc
> 1,
2978 "USER", "Username of session to remove filters from", NULL
);
2980 if (!config
->cluster_iam_master
)
2982 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2983 fmtaddr(config
->cluster_master_address
, 0));
2990 cli_print(cli
, "Specify a user to remove filters from");
2994 for (i
= 0; i
< argc
; i
++)
2996 if (!(s
= sessionbyuser(argv
[i
])))
2998 cli_print(cli
, "User %s is not connected", argv
[i
]);
3002 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3004 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3005 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3009 cli_print(cli
, "User %s not filtered", argv
[i
]);
3016 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3020 if (CLI_HELP_REQUESTED
)
3021 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3025 cli_print(cli
, "Specify a filter name");
3029 for (i
= 0; i
< argc
; i
++)
3031 int f
= find_access_list(argv
[i
]);
3032 ip_filter_rulet
*rules
;
3034 if (f
< 0 || !*ip_filters
[f
].name
)
3036 cli_print(cli
, "Access-list %s not defined", argv
[i
]);
3043 cli_print(cli
, "%s IP access list %s",
3044 ip_filters
[f
].extended
? "Extended" : "Standard",
3045 ip_filters
[f
].name
);
3047 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3049 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3051 cli_print(cli
, "%s (%d match%s)", r
,
3052 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3054 cli_print(cli
, "%s", r
);
3061 // Convert a string in the form of abcd.ef12.3456 into char[6]
3062 void parsemac(char *string
, char mac
[6])
3064 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)
3066 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)