1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.51 2005/01/13 08:26:25 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
);
102 static int regular_stuff(struct cli_def
*cli
);
103 static void parsemac(char *string
, char mac
[6]);
106 #define MODE_CONFIG_BGP 8
107 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
110 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
111 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
112 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
113 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
116 #define MODE_CONFIG_NACL 9
117 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
120 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
121 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
122 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
124 /* match if b is a substr of a */
125 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
127 void init_cli(char *hostname
)
131 struct cli_command
*c
;
132 struct cli_command
*c2
;
134 struct sockaddr_in addr
;
137 if (hostname
&& *hostname
)
138 cli_set_hostname(cli
, hostname
);
140 cli_set_hostname(cli
, "l2tpns");
142 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
143 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
145 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
147 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
148 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
149 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
150 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
151 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
152 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
153 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
154 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
155 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
156 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
157 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");
158 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
159 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
161 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
162 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
163 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
166 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
168 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
169 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
172 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
174 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
175 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
176 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
178 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
179 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
180 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
181 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
184 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
185 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
188 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
189 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
190 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
191 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
192 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
195 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
196 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
198 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
199 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
201 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
202 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
204 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
206 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
207 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
210 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
211 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
212 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
213 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
215 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
216 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
218 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
219 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
221 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
223 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
224 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
226 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
227 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
229 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
230 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
231 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
233 // Enable regular processing
234 cli_regular(cli
, regular_stuff
);
236 if (!(f
= fopen(CLIUSERS
, "r")))
238 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
242 while (fgets(buf
, 4096, f
))
245 if (*buf
== '#') continue;
246 if ((p
= strchr(buf
, '\r'))) *p
= 0;
247 if ((p
= strchr(buf
, '\n'))) *p
= 0;
249 if (!(p
= strchr((char *)buf
, ':'))) continue;
251 if (!strcmp(buf
, "enable"))
253 cli_allow_enable(cli
, p
);
254 LOG(3, 0, 0, "Setting enable password\n");
258 cli_allow_user(cli
, buf
, p
);
259 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
265 memset(&addr
, 0, sizeof(addr
));
266 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
267 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
270 // Set cli fd as non-blocking
271 flags
= fcntl(clifd
, F_GETFL
, 0);
272 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
274 addr
.sin_family
= AF_INET
;
275 addr
.sin_port
= htons(23);
276 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
278 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
284 void cli_do(int sockfd
)
286 int require_auth
= 1;
287 struct sockaddr_in addr
;
288 int l
= sizeof(addr
);
290 if (fork_and_close()) return;
291 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
293 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
294 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
295 fmtaddr(addr
.sin_addr
.s_addr
, 0));
298 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
302 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
303 if (!cli
->users
) /* paranoia */
305 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
311 /* no username/pass required */
318 debug_rb_tail
= ringbuffer
->tail
;
320 memset(&debug_flags
, 0, sizeof(debug_flags
));
321 debug_flags
.critical
= 1;
323 cli_loop(cli
, sockfd
);
326 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
327 fmtaddr(addr
.sin_addr
.s_addr
, 0));
332 static void cli_print_log(struct cli_def
*cli
, char *string
)
334 LOG(3, 0, 0, "%s\n", string
);
337 void cli_do_file(FILE *fh
)
339 LOG(3, 0, 0, "Reading configuration file\n");
340 cli_print_callback(cli
, cli_print_log
);
341 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
342 cli_print_callback(cli
, NULL
);
345 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
356 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
358 int v
= va_arg(ap
, int);
359 snprintf(buf
, sizeof(buf
), entry
, v
);
365 desc
= va_arg(ap
, char *);
367 cli_print(cli
, " %-20s %s", p
, desc
);
369 cli_print(cli
, " %s", p
);
371 entry
= desc
? va_arg(ap
, char *) : 0;
376 cli_print(cli
, " <cr>");
381 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
385 if (CLI_HELP_REQUESTED
)
386 return cli_arg_help(cli
, 1,
387 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
393 // Show individual session
394 for (i
= 0; i
< argc
; i
++)
396 unsigned int s
, b_in
, b_out
;
398 if (s
<= 0 || s
>= MAXSESSION
)
400 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
403 cli_print(cli
, "\r\nSession %d:", s
);
404 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
405 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
406 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
407 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
408 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
409 cli_print(cli
, "\tUnique SID:\t%lu", session
[s
].unique_id
);
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
));
727 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
731 time_t l
= GET_STAT(last_reset
);
733 char *p
= strchr(t
, '\n');
737 cli_print(cli
, "Last counter reset %s", t
);
743 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
749 if (CLI_HELP_REQUESTED
)
750 return cli_arg_help(cli
, 1,
751 "tag", "Include CVS release tag",
752 "file", "Include file versions",
755 for (i
= 0; i
< argc
; i
++)
756 if (!strcmp(argv
[i
], "tag"))
758 else if (!strcmp(argv
[i
], "file"))
761 cli_print(cli
, "L2TPNS %s", VERSION
);
764 char const *p
= strchr(cvs_name
, ':');
776 e
= strpbrk(p
, " \t$");
777 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
782 extern linked_list
*loaded_plugins
;
785 cli_print(cli
, "Files:");
786 cli_print(cli
, " %s", cvs_id_arp
);
788 cli_print(cli
, " %s", cvs_id_bgp
);
790 cli_print(cli
, " %s", cvs_id_cli
);
791 cli_print(cli
, " %s", cvs_id_cluster
);
792 cli_print(cli
, " %s", cvs_id_constants
);
793 cli_print(cli
, " %s", cvs_id_control
);
794 cli_print(cli
, " %s", cvs_id_icmp
);
795 cli_print(cli
, " %s", cvs_id_l2tpns
);
796 cli_print(cli
, " %s", cvs_id_ll
);
797 cli_print(cli
, " %s", cvs_id_md5
);
798 cli_print(cli
, " %s", cvs_id_ppp
);
799 cli_print(cli
, " %s", cvs_id_radius
);
800 cli_print(cli
, " %s", cvs_id_tbf
);
801 cli_print(cli
, " %s", cvs_id_util
);
803 ll_reset(loaded_plugins
);
804 while ((p
= ll_next(loaded_plugins
)))
806 char const **id
= dlsym(p
, "cvs_id");
808 cli_print(cli
, " %s", *id
);
815 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
818 int used
= 0, free
= 0, show_all
= 0;
820 if (!config
->cluster_iam_master
)
822 cli_print(cli
, "Can't do this on a slave. Do it on %s",
823 fmtaddr(config
->cluster_master_address
, 0));
828 if (CLI_HELP_REQUESTED
)
831 return cli_arg_help(cli
, 1, NULL
);
833 return cli_arg_help(cli
, 1,
834 "all", "Show all pool addresses, including unused",
838 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
842 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
843 for (i
= 0; i
< MAXIPPOOL
; i
++)
845 if (!ip_address_pool
[i
].address
) continue;
846 if (ip_address_pool
[i
].assigned
)
848 cli_print(cli
, "%-15s\tY %8d %s",
849 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
850 ip_address_pool
[i
].session
,
851 session
[ip_address_pool
[i
].session
].user
);
857 if (ip_address_pool
[i
].last
)
858 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
859 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
860 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
863 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
870 cli_print(cli
, "(Not displaying unused addresses)");
872 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
876 static FILE *save_config_fh
= 0;
877 static void print_save_config(struct cli_def
*cli
, char *string
)
880 fprintf(save_config_fh
, "%s\n", string
);
883 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
885 if (CLI_HELP_REQUESTED
)
886 return CLI_HELP_NO_ARGS
;
888 if ((save_config_fh
= fopen(config
->config_file
, "w")))
890 cli_print(cli
, "Writing configuration");
891 cli_print_callback(cli
, print_save_config
);
892 cmd_show_run(cli
, command
, argv
, argc
);
893 cli_print_callback(cli
, NULL
);
894 fclose(save_config_fh
);
899 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
904 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
906 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
909 char ipv6addr
[INET6_ADDRSTRLEN
];
911 if (CLI_HELP_REQUESTED
)
912 return CLI_HELP_NO_ARGS
;
914 cli_print(cli
, "# Current configuration:");
916 for (i
= 0; config_values
[i
].key
; i
++)
918 void *value
= ((void *)config
) + config_values
[i
].offset
;
919 if (config_values
[i
].type
== STRING
)
920 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
921 else if (config_values
[i
].type
== IPv4
)
922 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
923 else if (config_values
[i
].type
== IPv6
)
924 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
925 else if (config_values
[i
].type
== SHORT
)
926 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
927 else if (config_values
[i
].type
== BOOL
)
928 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
929 else if (config_values
[i
].type
== INT
)
930 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
931 else if (config_values
[i
].type
== UNSIGNED_LONG
)
932 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
933 else if (config_values
[i
].type
== MAC
)
934 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
935 *(unsigned short *) (value
+ 0),
936 *(unsigned short *) (value
+ 1),
937 *(unsigned short *) (value
+ 2),
938 *(unsigned short *) (value
+ 3),
939 *(unsigned short *) (value
+ 4),
940 *(unsigned short *) (value
+ 5));
943 cli_print(cli
, "# Plugins");
944 for (i
= 0; i
< MAXPLUGINS
; i
++)
946 if (*config
->plugins
[i
])
948 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
953 if (config
->as_number
)
958 cli_print(cli
, "# BGP");
959 cli_print(cli
, "router bgp %u", config
->as_number
);
960 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
962 if (!config
->neighbour
[i
].name
[0])
965 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
967 k
= config
->neighbour
[i
].keepalive
;
968 h
= config
->neighbour
[i
].hold
;
975 k
= BGP_KEEPALIVE_TIME
;
981 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
986 cli_print(cli
, "# Filters");
987 for (i
= 0; i
< MAXFILTER
; i
++)
989 ip_filter_rulet
*rules
;
990 if (!*ip_filters
[i
].name
)
993 cli_print(cli
, "ip access-list %s %s",
994 ip_filters
[i
].extended
? "extended" : "standard",
997 rules
= ip_filters
[i
].rules
;
998 while (rules
->action
)
999 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1002 cli_print(cli
, "# end");
1006 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1008 int i
, free
= 0, used
= 0, show_all
= 0;
1019 if (CLI_HELP_REQUESTED
)
1022 return cli_arg_help(cli
, 1, NULL
);
1024 return cli_arg_help(cli
, 1,
1025 "all", "Show all RADIUS sessions, including unused",
1029 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1033 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1036 for (i
= 1; i
< MAXRADIUS
; i
++)
1038 if (radius
[i
].state
== RADIUSNULL
)
1043 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1045 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1049 states
[radius
[i
].state
],
1055 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1060 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1064 if (CLI_HELP_REQUESTED
)
1065 return CLI_HELP_NO_ARGS
;
1067 cli_print(cli
, "Plugins currently loaded:");
1068 for (i
= 0; i
< MAXPLUGINS
; i
++)
1069 if (*config
->plugins
[i
])
1070 cli_print(cli
, " %s", config
->plugins
[i
]);
1075 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1079 if (CLI_HELP_REQUESTED
)
1080 return CLI_HELP_NO_ARGS
;
1082 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1091 for (i
= 0; i
< MAXSESSION
; i
++)
1093 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1094 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1098 session
[i
].throttle_in
,
1099 session
[i
].throttle_out
,
1101 session
[i
].tbf_out
);
1107 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1109 if (CLI_HELP_REQUESTED
)
1110 return CLI_HELP_NO_ARGS
;
1112 cli_print(cli
, " _\n"
1118 " ( \\ `. `-. _,.-:\\\n"
1119 " \\ \\ `. `-._ __..--' ,-';/\n"
1120 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1121 " `. `. `-._ __..--' ,' /\n"
1122 " `. `-_ ``--..'' _.-' ,'\n"
1123 " `-_ `-.___ __,--' ,'\n"
1124 " `-.__ `----\"\"\" __.-'\n"
1125 "hh `--..____..--'");
1130 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1132 if (CLI_HELP_REQUESTED
)
1133 return CLI_HELP_NO_ARGS
;
1135 memset(_statistics
, 0, sizeof(struct Tstats
));
1136 SET_STAT(last_reset
, time(NULL
));
1138 cli_print(cli
, "Counters cleared");
1142 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1147 if (CLI_HELP_REQUESTED
)
1148 return cli_arg_help(cli
, argc
> 1,
1149 "USER", "Username of session to drop", NULL
);
1151 if (!config
->cluster_iam_master
)
1153 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1154 fmtaddr(config
->cluster_master_address
, 0));
1161 cli_print(cli
, "Specify a user to drop");
1165 for (i
= 0; i
< argc
; i
++)
1167 if (!(s
= sessionbyuser(argv
[i
])))
1169 cli_print(cli
, "User %s is not connected", argv
[i
]);
1173 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1175 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1176 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1183 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1188 if (CLI_HELP_REQUESTED
)
1189 return cli_arg_help(cli
, argc
> 1,
1190 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1192 if (!config
->cluster_iam_master
)
1194 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1195 fmtaddr(config
->cluster_master_address
, 0));
1202 cli_print(cli
, "Specify a tunnel to drop");
1206 for (i
= 0; i
< argc
; i
++)
1208 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1210 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1216 cli_print(cli
, "Tunnel %d is not connected", t
);
1222 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1226 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1227 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1233 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1238 if (CLI_HELP_REQUESTED
)
1239 return cli_arg_help(cli
, argc
> 1,
1240 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1242 if (!config
->cluster_iam_master
)
1244 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1245 fmtaddr(config
->cluster_master_address
, 0));
1252 cli_print(cli
, "Specify a session id to drop");
1256 for (i
= 0; i
< argc
; i
++)
1258 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1260 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1264 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1266 cli_print(cli
, "Dropping session %d", s
);
1267 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1271 cli_print(cli
, "Session %d is not active.", s
);
1278 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1284 if (CLI_HELP_REQUESTED
)
1289 return cli_arg_help(cli
, 0,
1290 "USER", "Username of session to snoop", NULL
);
1293 return cli_arg_help(cli
, 0,
1294 "A.B.C.D", "IP address of snoop destination", NULL
);
1297 return cli_arg_help(cli
, 0,
1298 "N", "Port of snoop destination", NULL
);
1302 return cli_arg_help(cli
, 1, NULL
);
1309 if (!config
->cluster_iam_master
)
1311 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1312 fmtaddr(config
->cluster_master_address
, 0));
1319 cli_print(cli
, "Specify username, ip and port");
1323 if (!(s
= sessionbyuser(argv
[0])))
1325 cli_print(cli
, "User %s is not connected", argv
[0]);
1329 ip
= inet_addr(argv
[1]);
1330 if (!ip
|| ip
== INADDR_NONE
)
1332 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1336 port
= atoi(argv
[2]);
1339 cli_print(cli
, "Invalid port %s", argv
[2]);
1343 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1344 cli_session_actions
[s
].snoop_ip
= ip
;
1345 cli_session_actions
[s
].snoop_port
= port
;
1346 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1351 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1356 if (CLI_HELP_REQUESTED
)
1357 return cli_arg_help(cli
, argc
> 1,
1358 "USER", "Username of session to unsnoop", NULL
);
1360 if (!config
->cluster_iam_master
)
1362 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1363 fmtaddr(config
->cluster_master_address
, 0));
1370 cli_print(cli
, "Specify a user to unsnoop");
1374 for (i
= 0; i
< argc
; i
++)
1376 if (!(s
= sessionbyuser(argv
[i
])))
1378 cli_print(cli
, "User %s is not connected", argv
[i
]);
1382 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1383 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1389 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1396 throttle USER - throttle in/out to default rate
1397 throttle USER RATE - throttle in/out to default rate
1398 throttle USER in RATE - throttle input only
1399 throttle USER out RATE - throttle output only
1400 throttle USER in RATE out RATE - throttle both
1403 if (CLI_HELP_REQUESTED
)
1408 return cli_arg_help(cli
, 0,
1409 "USER", "Username of session to throttle", NULL
);
1412 return cli_arg_help(cli
, 1,
1413 "RATE", "Rate in kbps (in and out)",
1414 "in", "Select incoming rate",
1415 "out", "Select outgoing rate", NULL
);
1418 return cli_arg_help(cli
, 1,
1419 "in", "Select incoming rate",
1420 "out", "Select outgoing rate", NULL
);
1423 if (isdigit(argv
[1][0]))
1424 return cli_arg_help(cli
, 1, NULL
);
1427 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1430 return cli_arg_help(cli
, argc
> 1, NULL
);
1434 if (!config
->cluster_iam_master
)
1436 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1437 fmtaddr(config
->cluster_master_address
, 0));
1444 cli_print(cli
, "Specify a user to throttle");
1448 if (!(s
= sessionbyuser(argv
[0])))
1450 cli_print(cli
, "User %s is not connected", argv
[0]);
1456 rate_in
= rate_out
= config
->rl_rate
;
1460 rate_in
= rate_out
= atoi(argv
[1]);
1463 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1467 else if (argc
== 3 || argc
== 5)
1470 for (i
= 1; i
< argc
- 1; i
+= 2)
1473 if (MATCH("in", argv
[i
]))
1474 r
= rate_in
= atoi(argv
[i
+1]);
1475 else if (MATCH("out", argv
[i
]))
1476 r
= rate_out
= atoi(argv
[i
+1]);
1480 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1487 cli_print(cli
, "Invalid arguments");
1491 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1493 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1497 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1498 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1499 cli_session_actions
[s
].throttle_in
= rate_in
;
1501 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1502 cli_session_actions
[s
].throttle_out
= rate_out
;
1504 if (cli_session_actions
[s
].throttle_in
== -1 &&
1505 cli_session_actions
[s
].throttle_out
== -1)
1507 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1511 cli_print(cli
, "Throttling user %s", argv
[0]);
1512 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1517 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1522 if (CLI_HELP_REQUESTED
)
1523 return cli_arg_help(cli
, argc
> 1,
1524 "USER", "Username of session to unthrottle", NULL
);
1526 if (!config
->cluster_iam_master
)
1528 cli_print(cli
, "Can't do this on a slave. Do it on %s",
1529 fmtaddr(config
->cluster_master_address
, 0));
1536 cli_print(cli
, "Specify a user to unthrottle");
1540 for (i
= 0; i
< argc
; i
++)
1542 if (!(s
= sessionbyuser(argv
[i
])))
1544 cli_print(cli
, "User %s is not connected", argv
[i
]);
1548 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1550 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1551 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1555 cli_print(cli
, "User %s not throttled", argv
[i
]);
1562 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1566 if (CLI_HELP_REQUESTED
)
1567 return cli_arg_help(cli
, 1,
1568 "all", "Enable debugging for all except \"data\"",
1569 "critical", "", // FIXME: add descriptions
1579 char *p
= (char *) &debug_flags
;
1580 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1584 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1585 (debug_flags
.critical
) ? " critical" : "",
1586 (debug_flags
.error
) ? " error" : "",
1587 (debug_flags
.warning
) ? " warning" : "",
1588 (debug_flags
.info
) ? " info" : "",
1589 (debug_flags
.calls
) ? " calls" : "",
1590 (debug_flags
.data
) ? " data" : "");
1596 cli_print(cli
, "Debugging off");
1600 for (i
= 0; i
< argc
; i
++)
1602 int len
= strlen(argv
[i
]);
1604 if (argv
[i
][0] == 'c' && len
< 2)
1605 len
= 2; /* distinguish [cr]itical from [ca]lls */
1607 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1608 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1609 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1610 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1611 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1612 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1613 if (!strncmp("all", argv
[i
], len
))
1615 memset(&debug_flags
, 1, sizeof(debug_flags
));
1616 debug_flags
.data
= 0;
1620 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1626 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1630 if (CLI_HELP_REQUESTED
)
1631 return cli_arg_help(cli
, 1,
1632 "all", "Disable all debugging",
1633 "critical", "", // FIXME: add descriptions
1643 memset(&debug_flags
, 0, sizeof(debug_flags
));
1647 for (i
= 0; i
< argc
; i
++)
1649 int len
= strlen(argv
[i
]);
1651 if (argv
[i
][0] == 'c' && len
< 2)
1652 len
= 2; /* distinguish [cr]itical from [ca]lls */
1654 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1655 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1656 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1657 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1658 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1659 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1660 if (!strncmp("all", argv
[i
], len
))
1662 memset(&debug_flags
, 0, sizeof(debug_flags
));
1666 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1672 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1674 int i
, firstfree
= 0;
1676 if (CLI_HELP_REQUESTED
)
1677 return cli_arg_help(cli
, argc
> 1,
1678 "PLUGIN", "Name of plugin to load", NULL
);
1682 cli_print(cli
, "Specify a plugin to load");
1686 for (i
= 0; i
< MAXPLUGINS
; i
++)
1688 if (!*config
->plugins
[i
] && !firstfree
)
1690 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1692 cli_print(cli
, "Plugin is already loaded");
1699 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1700 config
->reload_config
= 1;
1701 cli_print(cli
, "Loading plugin %s", argv
[0]);
1707 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1711 if (CLI_HELP_REQUESTED
)
1712 return cli_arg_help(cli
, argc
> 1,
1713 "PLUGIN", "Name of plugin to unload", NULL
);
1717 cli_print(cli
, "Specify a plugin to remove");
1721 for (i
= 0; i
< MAXPLUGINS
; i
++)
1723 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1725 config
->reload_config
= 1;
1726 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1731 cli_print(cli
, "Plugin is not loaded");
1735 static char *duration(time_t secs
)
1737 static char *buf
= NULL
;
1740 if (!buf
) buf
= calloc(64, 1);
1744 int days
= secs
/ 86400;
1745 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1751 int mins
= secs
/ 60;
1752 int hrs
= mins
/ 60;
1755 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1757 else if (secs
>= 60)
1759 int mins
= secs
/ 60;
1760 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1763 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1768 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1771 char buf
[100], *p
= buf
, *loads
[3];
1772 int i
, num_sessions
= 0;
1774 if (CLI_HELP_REQUESTED
)
1775 return CLI_HELP_NO_ARGS
;
1777 fh
= fopen("/proc/loadavg", "r");
1778 fgets(buf
, 100, fh
);
1781 for (i
= 0; i
< 3; i
++)
1782 loads
[i
] = strdup(strsep(&p
, " "));
1785 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1787 for (i
= 1; i
< MAXSESSION
; i
++)
1788 if (session
[i
].opened
) num_sessions
++;
1790 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1792 duration(time_now
- config
->start_time
),
1794 loads
[0], loads
[1], loads
[2]
1796 for (i
= 0; i
< 3; i
++)
1797 if (loads
[i
]) free(loads
[i
]);
1799 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1804 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1808 if (CLI_HELP_REQUESTED
)
1814 int len
= strlen(argv
[0])-1;
1815 for (i
= 0; config_values
[i
].key
; i
++)
1816 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1817 cli_print(cli
, " %s", config_values
[i
].key
);
1823 return cli_arg_help(cli
, 0,
1824 "VALUE", "Value for variable", NULL
);
1828 return cli_arg_help(cli
, 1, NULL
);
1837 cli_print(cli
, "Specify variable and value");
1841 for (i
= 0; config_values
[i
].key
; i
++)
1843 void *value
= ((void *) config
) + config_values
[i
].offset
;
1844 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1846 // Found a value to set
1847 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1848 switch (config_values
[i
].type
)
1851 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1854 *(int *) value
= atoi(argv
[1]);
1857 *(unsigned long *) value
= atol(argv
[1]);
1860 *(short *) value
= atoi(argv
[1]);
1863 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1866 inet_pton(AF_INET6
, argv
[1], value
);
1869 parsemac(argv
[1], (char *)value
);
1872 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1878 cli_print(cli
, "Unknown variable type");
1881 config
->reload_config
= 1;
1886 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1890 int regular_stuff(struct cli_def
*cli
)
1896 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1898 char *m
= ringbuffer
->buffer
[i
].message
;
1904 switch (ringbuffer
->buffer
[i
].level
)
1906 case 0: show
= debug_flags
.critical
; break;
1907 case 1: show
= debug_flags
.error
; break;
1908 case 2: show
= debug_flags
.warning
; break;
1909 case 3: show
= debug_flags
.info
; break;
1910 case 4: show
= debug_flags
.calls
; break;
1911 case 5: show
= debug_flags
.data
; break;
1914 if (!show
) continue;
1916 if (!(p
= strchr(m
, '\n')))
1919 cli_print(cli
, "\r%s-%u-%u %.*s",
1920 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1921 ringbuffer
->buffer
[i
].tunnel
,
1922 ringbuffer
->buffer
[i
].session
,
1928 debug_rb_tail
= ringbuffer
->tail
;
1936 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1940 if (CLI_HELP_REQUESTED
)
1941 return cli_arg_help(cli
, argc
> 1,
1942 "<1-65535>", "Autonomous system number", NULL
);
1944 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1946 cli_print(cli
, "Invalid autonomous system number");
1950 if (bgp_configured
&& as
!= config
->as_number
)
1952 cli_print(cli
, "Can't change local AS on a running system");
1956 config
->as_number
= as
;
1957 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1962 static int find_bgp_neighbour(char const *name
)
1967 in_addr_t addrs
[4] = { 0 };
1970 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1973 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1974 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1976 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1978 if (!config
->neighbour
[i
].name
[0])
1980 if (new == -1) new = i
;
1984 if (!strcmp(name
, config
->neighbour
[i
].name
))
1987 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1990 for (a
= h
->h_addr_list
; *a
; a
++)
1993 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1994 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2002 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2008 if (CLI_HELP_REQUESTED
)
2013 return cli_arg_help(cli
, 0,
2014 "A.B.C.D", "BGP neighbour address",
2015 "NAME", "BGP neighbour name",
2019 return cli_arg_help(cli
, 0,
2020 "remote-as", "Set remote autonomous system number",
2021 "timers", "Set timers",
2025 if (MATCH("remote-as", argv
[1]))
2026 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2028 if (MATCH("timers", argv
[1]))
2031 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2034 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2036 if (argc
== 5 && !argv
[4][1])
2037 return cli_arg_help(cli
, 1, NULL
);
2046 cli_print(cli
, "Invalid arguments");
2050 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2052 cli_print(cli
, "Invalid neighbour");
2058 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2062 if (MATCH("remote-as", argv
[1]))
2064 int as
= atoi(argv
[2]);
2065 if (as
< 0 || as
> 65535)
2067 cli_print(cli
, "Invalid autonomous system number");
2071 if (!config
->neighbour
[i
].name
[0])
2073 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2074 config
->neighbour
[i
].keepalive
= -1;
2075 config
->neighbour
[i
].hold
= -1;
2078 config
->neighbour
[i
].as
= as
;
2082 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2084 cli_print(cli
, "Invalid arguments");
2088 if (!config
->neighbour
[i
].name
[0])
2090 cli_print(cli
, "Specify remote-as first");
2094 keepalive
= atoi(argv
[2]);
2095 hold
= atoi(argv
[3]);
2097 if (keepalive
< 1 || keepalive
> 65535)
2099 cli_print(cli
, "Invalid keepalive time");
2103 if (hold
< 3 || hold
> 65535)
2105 cli_print(cli
, "Invalid hold time");
2109 if (keepalive
== BGP_KEEPALIVE_TIME
)
2110 keepalive
= -1; // using default value
2112 if (hold
== BGP_HOLD_TIME
)
2115 config
->neighbour
[i
].keepalive
= keepalive
;
2116 config
->neighbour
[i
].hold
= hold
;
2121 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2125 if (CLI_HELP_REQUESTED
)
2126 return cli_arg_help(cli
, argc
> 0,
2127 "A.B.C.D", "BGP neighbour address",
2128 "NAME", "BGP neighbour name",
2133 cli_print(cli
, "Specify a BGP neighbour");
2137 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2139 cli_print(cli
, "Invalid neighbour");
2143 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2145 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2149 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2153 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2159 if (!bgp_configured
)
2162 if (CLI_HELP_REQUESTED
)
2163 return cli_arg_help(cli
, 1,
2164 "A.B.C.D", "BGP neighbour address",
2165 "NAME", "BGP neighbour name",
2168 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2169 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2173 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2175 if (!*bgp_peers
[i
].name
)
2178 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2179 if (argc
&& strcmp(addr
, argv
[0]) &&
2180 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2186 cli_print(cli
, "Peer AS Address "
2187 "State Retries Retry in Route Pend Timers");
2188 cli_print(cli
, "------------------ ----- --------------- "
2189 "----------- ------- -------- ----- ---- ---------");
2192 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2196 bgp_state_str(bgp_peers
[i
].state
),
2197 bgp_peers
[i
].retry_count
,
2198 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2199 bgp_peers
[i
].routing
? "yes" : "no",
2200 bgp_peers
[i
].update_routes
? "yes" : "no",
2201 bgp_peers
[i
].keepalive
,
2208 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2213 if (!bgp_configured
)
2216 if (CLI_HELP_REQUESTED
)
2217 return cli_arg_help(cli
, 1,
2218 "A.B.C.D", "BGP neighbour address",
2219 "NAME", "BGP neighbour name",
2222 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2224 if (bgp_peers
[i
].state
!= Established
)
2227 if (!bgp_peers
[i
].routing
)
2230 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2231 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2234 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2235 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2241 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2246 if (!bgp_configured
)
2249 if (CLI_HELP_REQUESTED
)
2250 return cli_arg_help(cli
, 1,
2251 "A.B.C.D", "BGP neighbour address",
2252 "NAME", "BGP neighbour name",
2255 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2257 if (bgp_peers
[i
].state
!= Established
)
2260 if (bgp_peers
[i
].routing
)
2263 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2264 if (argc
&& strcmp(addr
, argv
[0]) &&
2265 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2268 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2269 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2275 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2280 if (!bgp_configured
)
2283 if (CLI_HELP_REQUESTED
)
2284 return cli_arg_help(cli
, 1,
2285 "A.B.C.D", "BGP neighbour address",
2286 "NAME", "BGP neighbour name",
2289 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2291 if (!*bgp_peers
[i
].name
)
2294 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2295 if (argc
&& strcmp(addr
, argv
[0]) &&
2296 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2299 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2300 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2308 static int find_access_list(char const *name
)
2312 for (i
= 0; i
< MAXFILTER
; i
++)
2313 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2319 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2323 if (CLI_HELP_REQUESTED
)
2328 return cli_arg_help(cli
, 0,
2329 "standard", "Standard syntax",
2330 "extended", "Extended syntax",
2334 return cli_arg_help(cli
, argv
[1][1],
2335 "NAME", "Access-list name",
2339 if (argc
== 3 && !argv
[2][1])
2340 return cli_arg_help(cli
, 1, NULL
);
2348 cli_print(cli
, "Specify access-list type and name");
2352 if (MATCH("standard", argv
[0]))
2354 else if (MATCH("extended", argv
[0]))
2358 cli_print(cli
, "Invalid access-list type");
2362 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2363 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2365 cli_print(cli
, "Invalid access-list name");
2369 filt
= find_access_list(argv
[1]);
2374 cli_print(cli
, "Too many access-lists");
2379 if (!*ip_filters
[filt
].name
)
2381 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2382 strcpy(ip_filters
[filt
].name
, argv
[1]);
2383 ip_filters
[filt
].extended
= extended
;
2385 else if (ip_filters
[filt
].extended
!= extended
)
2387 cli_print(cli
, "Access-list is %s",
2388 ip_filters
[filt
].extended
? "extended" : "standard");
2393 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2397 if (filt
< 0 || !*ip_filters
[filt
].name
)
2399 cli_print(cli
, "Access-list not defined");
2404 if (ip_filters
[filt
].used
)
2406 cli_print(cli
, "Access-list in use");
2410 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2414 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2416 return access_list(cli
, argv
, argc
, 1);
2419 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2421 return access_list(cli
, argv
, argc
, 0);
2424 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2426 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2427 return sprintf(buf
, " any");
2429 if (wild
== INADDR_ANY
)
2430 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2432 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2435 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2439 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2440 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2441 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2442 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2443 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2449 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2451 static char buf
[256];
2454 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2457 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2458 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2461 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2465 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2466 p
+= show_ports(p
, &rule
->src_ports
);
2468 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2469 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2470 p
+= show_ports(p
, &rule
->dst_ports
);
2472 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2474 switch (rule
->tcp_flag_op
)
2476 case FILTER_FLAG_OP_EST
:
2477 p
+= sprintf(p
, " established");
2480 case FILTER_FLAG_OP_ANY
:
2481 case FILTER_FLAG_OP_ALL
:
2482 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2483 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2484 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2485 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2486 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2487 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2488 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2489 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2490 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2491 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2492 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2493 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2494 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2500 p
+= sprintf(p
, " fragments");
2505 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2507 static ip_filter_rulet rule
;
2508 struct in_addr addr
;
2512 if (CLI_HELP_REQUESTED
)
2516 cli_arg_help(cli
, 0,
2517 "ip", "Match IP packets",
2518 "tcp", "Match TCP packets",
2519 "udp", "Match UDP packets",
2525 // *sigh*, too darned complex
2526 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2532 cli_print(cli
, "Specify rule details");
2536 memset(&rule
, 0, sizeof(rule
));
2537 rule
.action
= (command
[0] == 'p')
2538 ? FILTER_ACTION_PERMIT
2539 : FILTER_ACTION_DENY
;
2541 if (MATCH("ip", argv
[0]))
2542 rule
.proto
= IPPROTO_IP
;
2543 else if (MATCH("udp", argv
[0]))
2544 rule
.proto
= IPPROTO_UDP
;
2545 else if (MATCH("tcp", argv
[0]))
2546 rule
.proto
= IPPROTO_TCP
;
2549 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2553 for (a
= 1, i
= 0; i
< 2; i
++)
2557 ip_filter_portt
*port
;
2562 wild
= &rule
.src_wild
;
2563 port
= &rule
.src_ports
;
2568 wild
= &rule
.dst_wild
;
2569 port
= &rule
.dst_ports
;
2572 cli_print(cli
, "Specify destination");
2577 if (MATCH("any", argv
[a
]))
2580 *wild
= INADDR_BROADCAST
;
2583 else if (MATCH("host", argv
[a
]))
2587 cli_print(cli
, "Specify host ip address");
2591 if (!inet_aton(argv
[a
], &addr
))
2593 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2605 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2609 if (!inet_aton(argv
[a
], &addr
))
2611 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2617 if (!inet_aton(argv
[++a
], &addr
))
2619 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2623 *wild
= addr
.s_addr
;
2627 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2631 if (MATCH("eq", argv
[a
]))
2632 port
->op
= FILTER_PORT_OP_EQ
;
2633 else if (MATCH("neq", argv
[a
]))
2634 port
->op
= FILTER_PORT_OP_NEQ
;
2635 else if (MATCH("gt", argv
[a
]))
2636 port
->op
= FILTER_PORT_OP_GT
;
2637 else if (MATCH("lt", argv
[a
]))
2638 port
->op
= FILTER_PORT_OP_LT
;
2639 else if (MATCH("range", argv
[a
]))
2640 port
->op
= FILTER_PORT_OP_RANGE
;
2647 cli_print(cli
, "Specify port");
2651 if (!(port
->port
= atoi(argv
[a
])))
2653 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2658 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2663 cli_print(cli
, "Specify port");
2667 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2669 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2676 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2678 if (MATCH("established", argv
[a
]))
2680 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2683 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2684 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2686 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2687 ? FILTER_FLAG_OP_ANY
2688 : FILTER_FLAG_OP_ALL
;
2692 cli_print(cli
, "Specify tcp flags");
2696 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2700 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2702 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2703 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2704 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2705 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2706 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2707 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2710 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2719 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2721 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2723 cli_print(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2733 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2740 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2742 static ip_filter_rulet rule
;
2743 struct in_addr addr
;
2745 if (CLI_HELP_REQUESTED
)
2749 cli_arg_help(cli
, argv
[0][1],
2750 "A.B.C.D", "Source address",
2751 "any", "Any source address",
2752 "host", "Source host",
2758 if (MATCH("any", argv
[0]))
2760 if (argc
== 2 && !argv
[1][1])
2761 cli_arg_help(cli
, 1, NULL
);
2763 else if (MATCH("host", argv
[0]))
2767 cli_arg_help(cli
, argv
[1][1],
2768 "A.B.C.D", "Host address",
2771 else if (argc
== 3 && !argv
[2][1])
2772 cli_arg_help(cli
, 1, NULL
);
2778 cli_arg_help(cli
, 1,
2779 "A.B.C.D", "Wildcard bits",
2782 else if (argc
== 3 && !argv
[2][1])
2783 cli_arg_help(cli
, 1, NULL
);
2791 cli_print(cli
, "Specify rule details");
2795 memset(&rule
, 0, sizeof(rule
));
2796 rule
.action
= (command
[0] == 'p')
2797 ? FILTER_ACTION_PERMIT
2798 : FILTER_ACTION_DENY
;
2800 rule
.proto
= IPPROTO_IP
;
2801 if (MATCH("any", argv
[0]))
2803 rule
.src_ip
= INADDR_ANY
;
2804 rule
.src_wild
= INADDR_BROADCAST
;
2806 else if (MATCH("host", argv
[0]))
2810 cli_print(cli
, "Specify host ip address");
2814 if (!inet_aton(argv
[1], &addr
))
2816 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2820 rule
.src_ip
= addr
.s_addr
;
2821 rule
.src_wild
= INADDR_ANY
;
2827 cli_print(cli
, "Specify source ip address and wildcard");
2831 if (!inet_aton(argv
[0], &addr
))
2833 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2837 rule
.src_ip
= addr
.s_addr
;
2841 if (!inet_aton(argv
[1], &addr
))
2843 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2847 rule
.src_wild
= addr
.s_addr
;
2850 rule
.src_wild
= INADDR_ANY
;
2856 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2859 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2860 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2861 : access_list_rule_std(cli
, command
, argv
, argc
);
2866 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2868 if (!ip_filters
[filt
].rules
[i
].action
)
2870 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2874 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2878 cli_print(cli
, "Too many rules");
2882 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2887 /* filter USER {in|out} FILTER ... */
2888 if (CLI_HELP_REQUESTED
)
2893 return cli_arg_help(cli
, 0,
2894 "USER", "Username of session to filter", NULL
);
2898 return cli_arg_help(cli
, 0,
2899 "in", "Set incoming filter",
2900 "out", "Set outgoing filter", NULL
);
2904 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2905 "NAME", "Filter name", NULL
);
2908 return cli_arg_help(cli
, argc
> 1, NULL
);
2912 if (!config
->cluster_iam_master
)
2914 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2915 fmtaddr(config
->cluster_master_address
, 0));
2920 if (argc
!= 3 && argc
!= 5)
2922 cli_print(cli
, "Specify a user and filters");
2926 if (!(s
= sessionbyuser(argv
[0])))
2928 cli_print(cli
, "User %s is not connected", argv
[0]);
2932 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2933 for (i
= 1; i
< argc
; i
+= 2)
2938 if (MATCH("in", argv
[i
]))
2940 if (session
[s
].filter_in
)
2942 cli_print(cli
, "Input already filtered");
2945 f
= &cli_session_actions
[s
].filter_in
;
2947 else if (MATCH("out", argv
[i
]))
2949 if (session
[s
].filter_out
)
2951 cli_print(cli
, "Output already filtered");
2954 f
= &cli_session_actions
[s
].filter_out
;
2958 cli_print(cli
, "Invalid filter specification");
2962 v
= find_access_list(argv
[i
+1]);
2963 if (v
< 0 || !*ip_filters
[v
].name
)
2965 cli_print(cli
, "Access-list %s not defined", argv
[i
+1]);
2972 cli_print(cli
, "Filtering user %s", argv
[0]);
2973 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2978 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2983 if (CLI_HELP_REQUESTED
)
2984 return cli_arg_help(cli
, argc
> 1,
2985 "USER", "Username of session to remove filters from", NULL
);
2987 if (!config
->cluster_iam_master
)
2989 cli_print(cli
, "Can't do this on a slave. Do it on %s",
2990 fmtaddr(config
->cluster_master_address
, 0));
2997 cli_print(cli
, "Specify a user to remove filters from");
3001 for (i
= 0; i
< argc
; i
++)
3003 if (!(s
= sessionbyuser(argv
[i
])))
3005 cli_print(cli
, "User %s is not connected", argv
[i
]);
3009 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3011 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3012 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3016 cli_print(cli
, "User %s not filtered", argv
[i
]);
3023 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3027 if (CLI_HELP_REQUESTED
)
3028 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3032 cli_print(cli
, "Specify a filter name");
3036 for (i
= 0; i
< argc
; i
++)
3038 int f
= find_access_list(argv
[i
]);
3039 ip_filter_rulet
*rules
;
3041 if (f
< 0 || !*ip_filters
[f
].name
)
3043 cli_print(cli
, "Access-list %s not defined", argv
[i
]);
3050 cli_print(cli
, "%s IP access list %s",
3051 ip_filters
[f
].extended
? "Extended" : "Standard",
3052 ip_filters
[f
].name
);
3054 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3056 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3058 cli_print(cli
, "%s (%d match%s)", r
,
3059 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3061 cli_print(cli
, "%s", r
);
3068 // Convert a string in the form of abcd.ef12.3456 into char[6]
3069 void parsemac(char *string
, char mac
[6])
3071 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)
3073 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)