1 // L2TPNS Command Line Interface
16 #include <arpa/inet.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
26 #include "constants.h"
35 extern tunnelt
*tunnel
;
36 extern bundlet
*bundle
;
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
;
64 static int debug_rb_tail
;
65 static char *debug_levels
[] = {
76 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 static int cmd_show_radius(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_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
104 static int regular_stuff(struct cli_def
*cli
);
107 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 #endif /* STATISTICS */
112 #define MODE_CONFIG_BGP 8
113 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
114 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
115 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
116 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
122 #define MODE_CONFIG_NACL 9
123 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
124 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
125 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
126 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
127 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
128 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
130 /* match if b is a substr of a */
131 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
133 void init_cli(char *hostname
)
137 struct cli_command
*c
;
138 struct cli_command
*c2
;
140 struct sockaddr_in addr
;
143 if (hostname
&& *hostname
)
144 cli_set_hostname(cli
, hostname
);
146 cli_set_hostname(cli
, "l2tpns");
148 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
149 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
151 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
153 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
154 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
155 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
156 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
157 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
158 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
159 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
160 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
161 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
162 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
163 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");
164 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
165 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
167 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
168 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
169 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
172 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
174 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
175 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
178 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
179 cli_register_command(cli
, NULL
, "shutdown", cmd_shutdown
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Shutdown l2tpns daemon and exit");
180 cli_register_command(cli
, NULL
, "reload", cmd_reload
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Reload configuration");
182 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
183 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
184 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
186 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
187 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
188 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
189 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
192 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
193 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
196 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
197 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
198 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
199 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
200 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
203 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
204 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
206 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
207 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
209 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
210 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
212 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
214 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
215 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
218 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
219 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
220 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
221 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
223 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
224 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
226 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
227 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
229 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
231 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
232 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
234 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
235 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
237 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
238 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
239 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
241 // Enable regular processing
242 cli_regular(cli
, regular_stuff
);
244 if (!(f
= fopen(CLIUSERS
, "r")))
246 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
250 while (fgets(buf
, 4096, f
))
253 if (*buf
== '#') continue;
254 if ((p
= strchr(buf
, '\r'))) *p
= 0;
255 if ((p
= strchr(buf
, '\n'))) *p
= 0;
257 if (!(p
= strchr((char *)buf
, ':'))) continue;
259 if (!strcmp(buf
, "enable"))
261 cli_allow_enable(cli
, p
);
262 LOG(3, 0, 0, "Setting enable password\n");
266 cli_allow_user(cli
, buf
, p
);
267 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
273 memset(&addr
, 0, sizeof(addr
));
274 clifd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
275 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
278 // Set cli fd as non-blocking
279 flags
= fcntl(clifd
, F_GETFL
, 0);
280 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
282 addr
.sin_family
= AF_INET
;
283 addr
.sin_port
= htons(23);
284 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
286 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
292 void cli_do(int sockfd
)
294 int require_auth
= 1;
295 struct sockaddr_in addr
;
296 socklen_t l
= sizeof(addr
);
298 if (fork_and_close()) return;
299 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
301 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
302 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
303 fmtaddr(addr
.sin_addr
.s_addr
, 0));
306 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
310 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
311 if (!cli
->users
) /* paranoia */
313 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
319 /* no username/pass required */
324 debug_rb_tail
= ringbuffer
->tail
;
326 memset(&debug_flags
, 0, sizeof(debug_flags
));
327 debug_flags
.critical
= 1;
329 cli_loop(cli
, sockfd
);
332 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
333 fmtaddr(addr
.sin_addr
.s_addr
, 0));
338 static void cli_print_log(struct cli_def
*cli
, char *string
)
340 LOG(3, 0, 0, "%s\n", string
);
343 void cli_do_file(FILE *fh
)
345 LOG(3, 0, 0, "Reading configuration file\n");
346 cli_print_callback(cli
, cli_print_log
);
347 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
348 cli_print_callback(cli
, NULL
);
351 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
362 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
364 int v
= va_arg(ap
, int);
365 snprintf(buf
, sizeof(buf
), entry
, v
);
371 desc
= va_arg(ap
, char *);
373 cli_error(cli
, " %-20s %s", p
, desc
);
375 cli_error(cli
, " %s", p
);
377 entry
= desc
? va_arg(ap
, char *) : 0;
382 cli_error(cli
, " <cr>");
387 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
391 if (CLI_HELP_REQUESTED
)
392 return cli_arg_help(cli
, 1,
393 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
399 // Show individual session
400 for (i
= 0; i
< argc
; i
++)
402 unsigned int s
, b_in
, b_out
;
404 if (s
<= 0 || s
>= MAXSESSION
)
406 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
409 cli_print(cli
, "\r\nSession %d:", s
);
410 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
411 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
412 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
413 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
414 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
415 switch (session
[s
].ppp
.phase
)
418 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
423 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
424 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
425 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
427 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
428 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
429 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
430 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
431 if (session
[s
].session_timeout
)
433 clockt opened
= session
[s
].opened
;
434 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
435 opened
= bundle
[session
[s
].bundle
].online_time
;
437 cli_print(cli
, "\tSess Timeout:\t%u seconds", session
[s
].session_timeout
- (opened
? abs(time_now
- opened
) : 0));
440 if (session
[s
].idle_timeout
)
441 cli_print(cli
, "\tIdle Timeout:\t%u seconds", session
[s
].idle_timeout
- (session
[s
].last_data
? abs(time_now
- session
[s
].last_data
) : 0));
443 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
444 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
445 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
446 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
447 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
448 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
449 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
450 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
451 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
452 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
453 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
455 cli_print(cli
, "\tIntercepted:\tno");
457 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
459 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
460 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
461 t
? "YES" : "no", t
? " (" : "",
462 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
464 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
468 b_in
= session
[s
].tbf_in
;
469 b_out
= session
[s
].tbf_out
;
471 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
472 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
473 "ByteSent", "PackSent", "PackDrop", "PackDelay");
476 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
478 (filter_list
[b_in
].next
? "*" : " "),
479 (b_in
< 100 ? "\t" : ""),
480 filter_list
[b_in
].rate
* 8,
481 filter_list
[b_in
].credit
,
482 filter_list
[b_in
].queued
,
483 filter_list
[b_in
].b_queued
,
484 filter_list
[b_in
].p_queued
,
485 filter_list
[b_in
].b_sent
,
486 filter_list
[b_in
].p_sent
,
487 filter_list
[b_in
].p_dropped
,
488 filter_list
[b_in
].p_delayed
);
491 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
493 (filter_list
[b_out
].next
? "*" : " "),
494 (b_out
< 100 ? "\t" : ""),
495 filter_list
[b_out
].rate
* 8,
496 filter_list
[b_out
].credit
,
497 filter_list
[b_out
].queued
,
498 filter_list
[b_out
].b_queued
,
499 filter_list
[b_out
].p_queued
,
500 filter_list
[b_out
].b_sent
,
501 filter_list
[b_out
].p_sent
,
502 filter_list
[b_out
].p_dropped
,
503 filter_list
[b_out
].p_delayed
);
510 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %-15s %s",
526 for (i
= 1; i
< MAXSESSION
; i
++)
528 if (!session
[i
].opened
) continue;
529 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %-15s %s",
532 session
[i
].user
[0] ? session
[i
].user
: "*",
533 fmtaddr(htonl(session
[i
].ip
), 0),
534 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
535 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
536 (session
[i
].walled_garden
) ? "Y" : "N",
537 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
538 abs(time_now
- (unsigned long)session
[i
].opened
),
539 (unsigned long)session
[i
].cout
,
540 (unsigned long)session
[i
].cin
,
541 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
542 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
543 session
[i
].calling
[0] ? session
[i
].calling
: "*");
548 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
550 int i
, x
, show_all
= 0;
558 if (CLI_HELP_REQUESTED
)
561 return cli_arg_help(cli
, 1,
562 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
565 return cli_arg_help(cli
, 1,
566 "all", "Show all tunnels, including unused",
567 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
574 if (strcmp(argv
[0], "all") == 0)
580 // Show individual tunnel
581 for (i
= 0; i
< argc
; i
++)
586 if (t
<= 0 || t
>= MAXTUNNEL
)
588 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
591 cli_print(cli
, "\r\nTunnel %d:", t
);
592 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
593 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
594 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
595 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
596 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
597 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
598 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
599 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
600 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
602 for (x
= 0; x
< MAXSESSION
; x
++)
603 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
604 sprintf(s
, "%s%u ", s
, x
);
606 cli_print(cli
, "\tSessions:\t%s", s
);
612 // Show tunnel summary
613 cli_print(cli
, "%4s %20s %20s %6s %s",
620 for (i
= 1; i
< MAXTUNNEL
; i
++)
623 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
625 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
626 cli_print(cli
, "%4d %20s %20s %6s %6d",
628 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
629 fmtaddr(htonl(tunnel
[i
].ip
), 0),
630 states
[tunnel
[i
].state
],
637 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
644 if (CLI_HELP_REQUESTED
)
645 return cli_arg_help(cli
, 1,
646 "USER", "Show details for specific username",
649 for (i
= 0; i
< MAXSESSION
; i
++)
651 if (!session
[i
].opened
) continue;
652 if (!session
[i
].user
[0]) continue;
656 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
658 if (strcmp(argv
[j
], session
[i
].user
) == 0)
660 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
661 sargv
[sargc
] = sid
[sargc
];
669 cli_print(cli
, "%s", session
[i
].user
);
673 return cmd_show_session(cli
, "users", sargv
, sargc
);
679 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
681 if (CLI_HELP_REQUESTED
)
682 return CLI_HELP_NO_ARGS
;
684 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
685 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
686 GET_STAT(tun_rx_bytes
),
687 GET_STAT(tun_rx_packets
),
688 GET_STAT(tun_rx_errors
),
689 GET_STAT(tun_rx_dropped
));
690 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
691 GET_STAT(tun_tx_bytes
),
692 GET_STAT(tun_tx_packets
),
693 GET_STAT(tun_tx_errors
));
696 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
697 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
698 GET_STAT(tunnel_rx_bytes
),
699 GET_STAT(tunnel_rx_packets
),
700 GET_STAT(tunnel_rx_errors
));
701 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
702 GET_STAT(tunnel_tx_bytes
),
703 GET_STAT(tunnel_tx_packets
),
704 GET_STAT(tunnel_tx_errors
),
705 GET_STAT(tunnel_retries
));
708 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
709 cli_print(cli
, "-----------------------------------------");
710 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
711 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
712 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
713 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
714 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
715 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
716 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
717 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
718 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
719 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
720 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
721 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
722 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
723 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
724 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
725 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
726 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
727 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
731 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
732 cli_print(cli
, "-----------------------------------------");
733 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
734 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
735 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
736 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
737 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
738 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
739 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
740 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
741 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
742 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
743 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
744 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
745 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
746 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
747 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
748 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
749 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
750 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
751 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
752 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
753 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
754 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
755 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
756 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
757 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
758 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
759 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
760 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
761 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
762 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
763 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
764 #endif /* STAT_CALLS */
767 time_t l
= GET_STAT(last_reset
);
769 char *p
= strchr(t
, '\n');
773 cli_print(cli
, "Last counter reset %s", t
);
779 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
781 if (CLI_HELP_REQUESTED
)
782 return CLI_HELP_NO_ARGS
;
784 memset(_statistics
, 0, sizeof(struct Tstats
));
785 SET_STAT(last_reset
, time(NULL
));
787 cli_print(cli
, "Counters cleared");
790 #endif /* STATISTICS */
792 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
794 if (CLI_HELP_REQUESTED
)
795 return CLI_HELP_NO_ARGS
;
797 cli_print(cli
, "L2TPNS %s", VERSION
);
801 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
804 int used
= 0, free
= 0, show_all
= 0;
806 if (!config
->cluster_iam_master
)
808 cli_print(cli
, "Can't do this on a slave. Do it on %s",
809 fmtaddr(config
->cluster_master_address
, 0));
814 if (CLI_HELP_REQUESTED
)
817 return cli_arg_help(cli
, 1, NULL
);
819 return cli_arg_help(cli
, 1,
820 "all", "Show all pool addresses, including unused",
824 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
828 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
829 for (i
= 0; i
< MAXIPPOOL
; i
++)
831 if (!ip_address_pool
[i
].address
) continue;
832 if (ip_address_pool
[i
].assigned
)
834 cli_print(cli
, "%-15s\tY %8d %s",
835 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
836 ip_address_pool
[i
].session
,
837 session
[ip_address_pool
[i
].session
].user
);
843 if (ip_address_pool
[i
].last
)
844 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
845 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
846 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
849 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
856 cli_print(cli
, "(Not displaying unused addresses)");
858 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
862 static FILE *save_config_fh
= 0;
863 static void print_save_config(struct cli_def
*cli
, char *string
)
866 fprintf(save_config_fh
, "%s\n", string
);
869 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
871 if (CLI_HELP_REQUESTED
)
872 return CLI_HELP_NO_ARGS
;
874 if ((save_config_fh
= fopen(config
->config_file
, "w")))
876 cli_print(cli
, "Writing configuration");
877 cli_print_callback(cli
, print_save_config
);
878 cmd_show_run(cli
, command
, argv
, argc
);
879 cli_print_callback(cli
, NULL
);
880 fclose(save_config_fh
);
885 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
890 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
892 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
895 char ipv6addr
[INET6_ADDRSTRLEN
];
897 if (CLI_HELP_REQUESTED
)
898 return CLI_HELP_NO_ARGS
;
900 cli_print(cli
, "# Current configuration:");
902 for (i
= 0; config_values
[i
].key
; i
++)
904 void *value
= ((void *)config
) + config_values
[i
].offset
;
905 if (config_values
[i
].type
== STRING
)
906 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
907 else if (config_values
[i
].type
== IPv4
)
908 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
909 else if (config_values
[i
].type
== IPv6
)
910 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
911 else if (config_values
[i
].type
== SHORT
)
912 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
913 else if (config_values
[i
].type
== BOOL
)
914 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
915 else if (config_values
[i
].type
== INT
)
916 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
917 else if (config_values
[i
].type
== UNSIGNED_LONG
)
918 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
921 cli_print(cli
, "# Plugins");
922 for (i
= 0; i
< MAXPLUGINS
; i
++)
924 if (*config
->plugins
[i
])
926 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
931 if (config
->as_number
)
936 cli_print(cli
, "# BGP");
937 cli_print(cli
, "router bgp %u", config
->as_number
);
938 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
940 if (!config
->neighbour
[i
].name
[0])
943 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
945 k
= config
->neighbour
[i
].keepalive
;
946 h
= config
->neighbour
[i
].hold
;
953 k
= BGP_KEEPALIVE_TIME
;
959 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
964 cli_print(cli
, "# Filters");
965 for (i
= 0; i
< MAXFILTER
; i
++)
967 ip_filter_rulet
*rules
;
968 if (!*ip_filters
[i
].name
)
971 cli_print(cli
, "ip access-list %s %s",
972 ip_filters
[i
].extended
? "extended" : "standard",
975 rules
= ip_filters
[i
].rules
;
976 while (rules
->action
)
977 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
980 cli_print(cli
, "# end");
984 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
986 int i
, free
= 0, used
= 0, show_all
= 0;
998 if (CLI_HELP_REQUESTED
)
1001 return cli_arg_help(cli
, 1, NULL
);
1003 return cli_arg_help(cli
, 1,
1004 "all", "Show all RADIUS sessions, including unused",
1008 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1012 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1015 for (i
= 1; i
< MAXRADIUS
; i
++)
1017 if (radius
[i
].state
== RADIUSNULL
)
1022 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1024 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1028 states
[radius
[i
].state
],
1034 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1039 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1043 if (CLI_HELP_REQUESTED
)
1044 return CLI_HELP_NO_ARGS
;
1046 cli_print(cli
, "Plugins currently loaded:");
1047 for (i
= 0; i
< MAXPLUGINS
; i
++)
1048 if (*config
->plugins
[i
])
1049 cli_print(cli
, " %s", config
->plugins
[i
]);
1054 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1058 if (CLI_HELP_REQUESTED
)
1059 return CLI_HELP_NO_ARGS
;
1061 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1070 for (i
= 0; i
< MAXSESSION
; i
++)
1072 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1073 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1077 session
[i
].throttle_in
,
1078 session
[i
].throttle_out
,
1080 session
[i
].tbf_out
);
1086 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1088 if (CLI_HELP_REQUESTED
)
1089 return CLI_HELP_NO_ARGS
;
1091 cli_print(cli
, " _\n"
1097 " ( \\ `. `-. _,.-:\\\n"
1098 " \\ \\ `. `-._ __..--' ,-';/\n"
1099 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1100 " `. `. `-._ __..--' ,' /\n"
1101 " `. `-_ ``--..'' _.-' ,'\n"
1102 " `-_ `-.___ __,--' ,'\n"
1103 " `-.__ `----\"\"\" __.-'\n"
1104 "hh `--..____..--'");
1109 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1114 if (CLI_HELP_REQUESTED
)
1115 return cli_arg_help(cli
, argc
> 1,
1116 "USER", "Username of session to drop", NULL
);
1118 if (!config
->cluster_iam_master
)
1120 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1121 fmtaddr(config
->cluster_master_address
, 0));
1128 cli_error(cli
, "Specify a user to drop");
1132 for (i
= 0; i
< argc
; i
++)
1134 if (!(s
= sessionbyuser(argv
[i
])))
1136 cli_error(cli
, "User %s is not connected", argv
[i
]);
1140 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1142 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1143 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1150 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1155 if (CLI_HELP_REQUESTED
)
1156 return cli_arg_help(cli
, argc
> 1,
1157 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1159 if (!config
->cluster_iam_master
)
1161 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1162 fmtaddr(config
->cluster_master_address
, 0));
1169 cli_error(cli
, "Specify a tunnel to drop");
1173 for (i
= 0; i
< argc
; i
++)
1175 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1177 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1183 cli_error(cli
, "Tunnel %d is not connected", t
);
1189 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1193 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1194 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1200 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1205 if (CLI_HELP_REQUESTED
)
1206 return cli_arg_help(cli
, argc
> 1,
1207 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1209 if (!config
->cluster_iam_master
)
1211 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1212 fmtaddr(config
->cluster_master_address
, 0));
1219 cli_error(cli
, "Specify a session id to drop");
1223 for (i
= 0; i
< argc
; i
++)
1225 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1227 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1231 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1233 cli_print(cli
, "Dropping session %d", s
);
1234 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1238 cli_error(cli
, "Session %d is not active.", s
);
1245 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1251 if (CLI_HELP_REQUESTED
)
1256 return cli_arg_help(cli
, 0,
1257 "USER", "Username of session to snoop", NULL
);
1260 return cli_arg_help(cli
, 0,
1261 "A.B.C.D", "IP address of snoop destination", NULL
);
1264 return cli_arg_help(cli
, 0,
1265 "N", "Port of snoop destination", NULL
);
1269 return cli_arg_help(cli
, 1, NULL
);
1276 if (!config
->cluster_iam_master
)
1278 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1279 fmtaddr(config
->cluster_master_address
, 0));
1286 cli_error(cli
, "Specify username, ip and port");
1290 if (!(s
= sessionbyuser(argv
[0])))
1292 cli_error(cli
, "User %s is not connected", argv
[0]);
1296 ip
= inet_addr(argv
[1]);
1297 if (!ip
|| ip
== INADDR_NONE
)
1299 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1303 port
= atoi(argv
[2]);
1306 cli_error(cli
, "Invalid port %s", argv
[2]);
1310 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1311 cli_session_actions
[s
].snoop_ip
= ip
;
1312 cli_session_actions
[s
].snoop_port
= port
;
1313 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1318 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1323 if (CLI_HELP_REQUESTED
)
1324 return cli_arg_help(cli
, argc
> 1,
1325 "USER", "Username of session to unsnoop", NULL
);
1327 if (!config
->cluster_iam_master
)
1329 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1330 fmtaddr(config
->cluster_master_address
, 0));
1337 cli_error(cli
, "Specify a user to unsnoop");
1341 for (i
= 0; i
< argc
; i
++)
1343 if (!(s
= sessionbyuser(argv
[i
])))
1345 cli_error(cli
, "User %s is not connected", argv
[i
]);
1349 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1350 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1356 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1363 throttle USER - throttle in/out to default rate
1364 throttle USER RATE - throttle in/out to default rate
1365 throttle USER in RATE - throttle input only
1366 throttle USER out RATE - throttle output only
1367 throttle USER in RATE out RATE - throttle both
1370 if (CLI_HELP_REQUESTED
)
1375 return cli_arg_help(cli
, 0,
1376 "USER", "Username of session to throttle", NULL
);
1379 return cli_arg_help(cli
, 1,
1380 "RATE", "Rate in kbps (in and out)",
1381 "in", "Select incoming rate",
1382 "out", "Select outgoing rate", NULL
);
1385 return cli_arg_help(cli
, 1,
1386 "in", "Select incoming rate",
1387 "out", "Select outgoing rate", NULL
);
1390 if (isdigit(argv
[1][0]))
1391 return cli_arg_help(cli
, 1, NULL
);
1394 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1397 return cli_arg_help(cli
, argc
> 1, NULL
);
1401 if (!config
->cluster_iam_master
)
1403 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1404 fmtaddr(config
->cluster_master_address
, 0));
1411 cli_error(cli
, "Specify a user to throttle");
1415 if (!(s
= sessionbyuser(argv
[0])))
1417 cli_error(cli
, "User %s is not connected", argv
[0]);
1423 rate_in
= rate_out
= config
->rl_rate
;
1427 rate_in
= rate_out
= atoi(argv
[1]);
1430 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1434 else if (argc
== 3 || argc
== 5)
1437 for (i
= 1; i
< argc
- 1; i
+= 2)
1440 if (MATCH("in", argv
[i
]))
1441 r
= rate_in
= atoi(argv
[i
+1]);
1442 else if (MATCH("out", argv
[i
]))
1443 r
= rate_out
= atoi(argv
[i
+1]);
1447 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1454 cli_error(cli
, "Invalid arguments");
1458 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1460 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1464 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1465 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1466 cli_session_actions
[s
].throttle_in
= rate_in
;
1468 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1469 cli_session_actions
[s
].throttle_out
= rate_out
;
1471 if (cli_session_actions
[s
].throttle_in
== -1 &&
1472 cli_session_actions
[s
].throttle_out
== -1)
1474 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1478 cli_print(cli
, "Throttling user %s", argv
[0]);
1479 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1484 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1489 if (CLI_HELP_REQUESTED
)
1490 return cli_arg_help(cli
, argc
> 1,
1491 "USER", "Username of session to unthrottle", NULL
);
1493 if (!config
->cluster_iam_master
)
1495 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1496 fmtaddr(config
->cluster_master_address
, 0));
1503 cli_error(cli
, "Specify a user to unthrottle");
1507 for (i
= 0; i
< argc
; i
++)
1509 if (!(s
= sessionbyuser(argv
[i
])))
1511 cli_error(cli
, "User %s is not connected", argv
[i
]);
1515 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1517 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1518 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1522 cli_error(cli
, "User %s not throttled", argv
[i
]);
1529 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1533 if (CLI_HELP_REQUESTED
)
1534 return cli_arg_help(cli
, 1,
1535 "all", "Enable debugging for all except \"data\"",
1536 "critical", "", // FIXME: add descriptions
1546 char *p
= (char *) &debug_flags
;
1547 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1551 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1552 (debug_flags
.critical
) ? " critical" : "",
1553 (debug_flags
.error
) ? " error" : "",
1554 (debug_flags
.warning
) ? " warning" : "",
1555 (debug_flags
.info
) ? " info" : "",
1556 (debug_flags
.calls
) ? " calls" : "",
1557 (debug_flags
.data
) ? " data" : "");
1563 cli_print(cli
, "Debugging off");
1567 for (i
= 0; i
< argc
; i
++)
1569 int len
= strlen(argv
[i
]);
1571 if (argv
[i
][0] == 'c' && len
< 2)
1572 len
= 2; /* distinguish [cr]itical from [ca]lls */
1574 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1575 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1576 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1577 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1578 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1579 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1580 if (!strncmp("all", argv
[i
], len
))
1582 memset(&debug_flags
, 1, sizeof(debug_flags
));
1583 debug_flags
.data
= 0;
1587 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1593 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1597 if (CLI_HELP_REQUESTED
)
1598 return cli_arg_help(cli
, 1,
1599 "all", "Disable all debugging",
1600 "critical", "", // FIXME: add descriptions
1610 memset(&debug_flags
, 0, sizeof(debug_flags
));
1614 for (i
= 0; i
< argc
; i
++)
1616 int len
= strlen(argv
[i
]);
1618 if (argv
[i
][0] == 'c' && len
< 2)
1619 len
= 2; /* distinguish [cr]itical from [ca]lls */
1621 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1622 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1623 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1624 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1625 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1626 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1627 if (!strncmp("all", argv
[i
], len
))
1629 memset(&debug_flags
, 0, sizeof(debug_flags
));
1633 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1639 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1641 int i
, firstfree
= 0;
1643 if (CLI_HELP_REQUESTED
)
1644 return cli_arg_help(cli
, argc
> 1,
1645 "PLUGIN", "Name of plugin to load", NULL
);
1649 cli_error(cli
, "Specify a plugin to load");
1653 for (i
= 0; i
< MAXPLUGINS
; i
++)
1655 if (!*config
->plugins
[i
] && !firstfree
)
1657 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1659 cli_error(cli
, "Plugin is already loaded");
1666 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1667 config
->reload_config
= 1;
1668 cli_print(cli
, "Loading plugin %s", argv
[0]);
1674 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1678 if (CLI_HELP_REQUESTED
)
1679 return cli_arg_help(cli
, argc
> 1,
1680 "PLUGIN", "Name of plugin to unload", NULL
);
1684 cli_error(cli
, "Specify a plugin to remove");
1688 for (i
= 0; i
< MAXPLUGINS
; i
++)
1690 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1692 config
->reload_config
= 1;
1693 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1698 cli_error(cli
, "Plugin is not loaded");
1702 static char *duration(time_t secs
)
1704 static char *buf
= NULL
;
1707 if (!buf
) buf
= calloc(64, 1);
1711 int days
= secs
/ 86400;
1712 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1718 int mins
= secs
/ 60;
1719 int hrs
= mins
/ 60;
1722 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1724 else if (secs
>= 60)
1726 int mins
= secs
/ 60;
1727 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1730 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1735 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1738 char buf
[100], *p
= buf
, *loads
[3];
1739 int i
, num_sessions
= 0;
1741 if (CLI_HELP_REQUESTED
)
1742 return CLI_HELP_NO_ARGS
;
1744 fh
= fopen("/proc/loadavg", "r");
1745 fgets(buf
, 100, fh
);
1748 for (i
= 0; i
< 3; i
++)
1749 loads
[i
] = strdup(strsep(&p
, " "));
1752 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1754 for (i
= 1; i
< MAXSESSION
; i
++)
1755 if (session
[i
].opened
) num_sessions
++;
1757 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1759 duration(time_now
- config
->start_time
),
1761 loads
[0], loads
[1], loads
[2]
1763 for (i
= 0; i
< 3; i
++)
1764 if (loads
[i
]) free(loads
[i
]);
1766 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1771 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1775 if (CLI_HELP_REQUESTED
)
1781 int len
= strlen(argv
[0])-1;
1782 for (i
= 0; config_values
[i
].key
; i
++)
1783 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1784 cli_error(cli
, " %s", config_values
[i
].key
);
1790 return cli_arg_help(cli
, 0,
1791 "VALUE", "Value for variable", NULL
);
1795 return cli_arg_help(cli
, 1, NULL
);
1804 cli_error(cli
, "Specify variable and value");
1808 for (i
= 0; config_values
[i
].key
; i
++)
1810 void *value
= ((void *) config
) + config_values
[i
].offset
;
1811 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1813 // Found a value to set
1814 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1815 switch (config_values
[i
].type
)
1818 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1821 *(int *) value
= atoi(argv
[1]);
1824 *(unsigned long *) value
= atol(argv
[1]);
1827 *(short *) value
= atoi(argv
[1]);
1830 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1833 inet_pton(AF_INET6
, argv
[1], value
);
1836 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1842 cli_error(cli
, "Unknown variable type");
1845 config
->reload_config
= 1;
1850 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1854 int regular_stuff(struct cli_def
*cli
)
1860 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1862 char *m
= ringbuffer
->buffer
[i
].message
;
1868 switch (ringbuffer
->buffer
[i
].level
)
1870 case 0: show
= debug_flags
.critical
; break;
1871 case 1: show
= debug_flags
.error
; break;
1872 case 2: show
= debug_flags
.warning
; break;
1873 case 3: show
= debug_flags
.info
; break;
1874 case 4: show
= debug_flags
.calls
; break;
1875 case 5: show
= debug_flags
.data
; break;
1878 if (!show
) continue;
1880 if (!(p
= strchr(m
, '\n')))
1883 cli_error(cli
, "\r%s-%u-%u %.*s",
1884 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1885 ringbuffer
->buffer
[i
].tunnel
,
1886 ringbuffer
->buffer
[i
].session
,
1892 debug_rb_tail
= ringbuffer
->tail
;
1900 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1904 if (CLI_HELP_REQUESTED
)
1905 return cli_arg_help(cli
, argc
> 1,
1906 "<1-65535>", "Autonomous system number", NULL
);
1908 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1910 cli_error(cli
, "Invalid autonomous system number");
1914 if (bgp_configured
&& as
!= config
->as_number
)
1916 cli_error(cli
, "Can't change local AS on a running system");
1920 config
->as_number
= as
;
1921 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1926 static int find_bgp_neighbour(char const *name
)
1931 in_addr_t addrs
[4] = { 0 };
1934 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1937 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1938 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1940 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1942 if (!config
->neighbour
[i
].name
[0])
1944 if (new == -1) new = i
;
1948 if (!strcmp(name
, config
->neighbour
[i
].name
))
1951 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1954 for (a
= h
->h_addr_list
; *a
; a
++)
1957 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1958 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1966 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1972 if (CLI_HELP_REQUESTED
)
1977 return cli_arg_help(cli
, 0,
1978 "A.B.C.D", "BGP neighbour address",
1979 "NAME", "BGP neighbour name",
1983 return cli_arg_help(cli
, 0,
1984 "remote-as", "Set remote autonomous system number",
1985 "timers", "Set timers",
1989 if (MATCH("remote-as", argv
[1]))
1990 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
1992 if (MATCH("timers", argv
[1]))
1995 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
1998 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2000 if (argc
== 5 && !argv
[4][1])
2001 return cli_arg_help(cli
, 1, NULL
);
2010 cli_error(cli
, "Invalid arguments");
2014 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2016 cli_error(cli
, "Invalid neighbour");
2022 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2026 if (MATCH("remote-as", argv
[1]))
2028 int as
= atoi(argv
[2]);
2029 if (as
< 0 || as
> 65535)
2031 cli_error(cli
, "Invalid autonomous system number");
2035 if (!config
->neighbour
[i
].name
[0])
2037 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2038 config
->neighbour
[i
].keepalive
= -1;
2039 config
->neighbour
[i
].hold
= -1;
2042 config
->neighbour
[i
].as
= as
;
2046 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2048 cli_error(cli
, "Invalid arguments");
2052 if (!config
->neighbour
[i
].name
[0])
2054 cli_error(cli
, "Specify remote-as first");
2058 keepalive
= atoi(argv
[2]);
2059 hold
= atoi(argv
[3]);
2061 if (keepalive
< 1 || keepalive
> 65535)
2063 cli_error(cli
, "Invalid keepalive time");
2067 if (hold
< 3 || hold
> 65535)
2069 cli_error(cli
, "Invalid hold time");
2073 if (keepalive
== BGP_KEEPALIVE_TIME
)
2074 keepalive
= -1; // using default value
2076 if (hold
== BGP_HOLD_TIME
)
2079 config
->neighbour
[i
].keepalive
= keepalive
;
2080 config
->neighbour
[i
].hold
= hold
;
2085 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2089 if (CLI_HELP_REQUESTED
)
2090 return cli_arg_help(cli
, argc
> 0,
2091 "A.B.C.D", "BGP neighbour address",
2092 "NAME", "BGP neighbour name",
2097 cli_error(cli
, "Specify a BGP neighbour");
2101 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2103 cli_error(cli
, "Invalid neighbour");
2107 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2109 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2113 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2117 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2123 if (!bgp_configured
)
2126 if (CLI_HELP_REQUESTED
)
2127 return cli_arg_help(cli
, 1,
2128 "A.B.C.D", "BGP neighbour address",
2129 "NAME", "BGP neighbour name",
2132 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2133 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2137 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2139 if (!*bgp_peers
[i
].name
)
2142 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2143 if (argc
&& strcmp(addr
, argv
[0]) &&
2144 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2150 cli_print(cli
, "Peer AS Address "
2151 "State Retries Retry in Route Pend Timers");
2152 cli_print(cli
, "------------------ ----- --------------- "
2153 "----------- ------- -------- ----- ---- ---------");
2156 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2160 bgp_state_str(bgp_peers
[i
].state
),
2161 bgp_peers
[i
].retry_count
,
2162 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2163 bgp_peers
[i
].routing
? "yes" : "no",
2164 bgp_peers
[i
].update_routes
? "yes" : "no",
2165 bgp_peers
[i
].keepalive
,
2172 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2177 if (!bgp_configured
)
2180 if (CLI_HELP_REQUESTED
)
2181 return cli_arg_help(cli
, 1,
2182 "A.B.C.D", "BGP neighbour address",
2183 "NAME", "BGP neighbour name",
2186 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2188 if (bgp_peers
[i
].state
!= Established
)
2191 if (!bgp_peers
[i
].routing
)
2194 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2195 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2198 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2199 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2205 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2210 if (!bgp_configured
)
2213 if (CLI_HELP_REQUESTED
)
2214 return cli_arg_help(cli
, 1,
2215 "A.B.C.D", "BGP neighbour address",
2216 "NAME", "BGP neighbour name",
2219 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2221 if (bgp_peers
[i
].state
!= Established
)
2224 if (bgp_peers
[i
].routing
)
2227 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2228 if (argc
&& strcmp(addr
, argv
[0]) &&
2229 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2232 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2233 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2239 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2244 if (!bgp_configured
)
2247 if (CLI_HELP_REQUESTED
)
2248 return cli_arg_help(cli
, 1,
2249 "A.B.C.D", "BGP neighbour address",
2250 "NAME", "BGP neighbour name",
2253 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2255 if (!*bgp_peers
[i
].name
)
2258 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2259 if (argc
&& strcmp(addr
, argv
[0]) &&
2260 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2263 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2264 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2272 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2276 if (CLI_HELP_REQUESTED
)
2281 return cli_arg_help(cli
, 0,
2282 "standard", "Standard syntax",
2283 "extended", "Extended syntax",
2287 return cli_arg_help(cli
, argv
[1][1],
2288 "NAME", "Access-list name",
2292 if (argc
== 3 && !argv
[2][1])
2293 return cli_arg_help(cli
, 1, NULL
);
2301 cli_error(cli
, "Specify access-list type and name");
2305 if (MATCH("standard", argv
[0]))
2307 else if (MATCH("extended", argv
[0]))
2311 cli_error(cli
, "Invalid access-list type");
2315 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2316 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2318 cli_error(cli
, "Invalid access-list name");
2322 filt
= find_filter(argv
[1], strlen(argv
[1]));
2327 cli_error(cli
, "Too many access-lists");
2332 if (!*ip_filters
[filt
].name
)
2334 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2335 strcpy(ip_filters
[filt
].name
, argv
[1]);
2336 ip_filters
[filt
].extended
= extended
;
2338 else if (ip_filters
[filt
].extended
!= extended
)
2340 cli_error(cli
, "Access-list is %s",
2341 ip_filters
[filt
].extended
? "extended" : "standard");
2346 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2350 if (filt
< 0 || !*ip_filters
[filt
].name
)
2352 cli_error(cli
, "Access-list not defined");
2357 if (ip_filters
[filt
].used
)
2359 cli_error(cli
, "Access-list in use");
2363 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2367 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2369 return access_list(cli
, argv
, argc
, 1);
2372 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2374 return access_list(cli
, argv
, argc
, 0);
2377 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2379 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2380 return sprintf(buf
, " any");
2382 if (wild
== INADDR_ANY
)
2383 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2385 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2388 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2392 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2393 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2394 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2395 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2396 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2402 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2404 static char buf
[256];
2407 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2410 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2411 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2414 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2418 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2419 p
+= show_ports(p
, &rule
->src_ports
);
2421 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2422 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2423 p
+= show_ports(p
, &rule
->dst_ports
);
2425 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2427 switch (rule
->tcp_flag_op
)
2429 case FILTER_FLAG_OP_EST
:
2430 p
+= sprintf(p
, " established");
2433 case FILTER_FLAG_OP_ANY
:
2434 case FILTER_FLAG_OP_ALL
:
2435 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2436 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2437 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2438 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2439 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2440 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2441 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2442 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2443 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2444 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2445 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2446 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2447 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2453 p
+= sprintf(p
, " fragments");
2458 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2460 static ip_filter_rulet rule
;
2461 struct in_addr addr
;
2465 if (CLI_HELP_REQUESTED
)
2469 cli_arg_help(cli
, 0,
2470 "ip", "Match IP packets",
2471 "tcp", "Match TCP packets",
2472 "udp", "Match UDP packets",
2478 // *sigh*, too darned complex
2479 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2485 cli_error(cli
, "Specify rule details");
2489 memset(&rule
, 0, sizeof(rule
));
2490 rule
.action
= (command
[0] == 'p')
2491 ? FILTER_ACTION_PERMIT
2492 : FILTER_ACTION_DENY
;
2494 if (MATCH("ip", argv
[0]))
2495 rule
.proto
= IPPROTO_IP
;
2496 else if (MATCH("udp", argv
[0]))
2497 rule
.proto
= IPPROTO_UDP
;
2498 else if (MATCH("tcp", argv
[0]))
2499 rule
.proto
= IPPROTO_TCP
;
2502 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2506 for (a
= 1, i
= 0; i
< 2; i
++)
2510 ip_filter_portt
*port
;
2515 wild
= &rule
.src_wild
;
2516 port
= &rule
.src_ports
;
2521 wild
= &rule
.dst_wild
;
2522 port
= &rule
.dst_ports
;
2525 cli_error(cli
, "Specify destination");
2530 if (MATCH("any", argv
[a
]))
2533 *wild
= INADDR_BROADCAST
;
2536 else if (MATCH("host", argv
[a
]))
2540 cli_error(cli
, "Specify host ip address");
2544 if (!inet_aton(argv
[a
], &addr
))
2546 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2558 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2562 if (!inet_aton(argv
[a
], &addr
))
2564 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2570 if (!inet_aton(argv
[++a
], &addr
))
2572 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2576 *wild
= addr
.s_addr
;
2580 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2584 if (MATCH("eq", argv
[a
]))
2585 port
->op
= FILTER_PORT_OP_EQ
;
2586 else if (MATCH("neq", argv
[a
]))
2587 port
->op
= FILTER_PORT_OP_NEQ
;
2588 else if (MATCH("gt", argv
[a
]))
2589 port
->op
= FILTER_PORT_OP_GT
;
2590 else if (MATCH("lt", argv
[a
]))
2591 port
->op
= FILTER_PORT_OP_LT
;
2592 else if (MATCH("range", argv
[a
]))
2593 port
->op
= FILTER_PORT_OP_RANGE
;
2600 cli_error(cli
, "Specify port");
2604 if (!(port
->port
= atoi(argv
[a
])))
2606 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2611 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2616 cli_error(cli
, "Specify port");
2620 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2622 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2629 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2631 if (MATCH("established", argv
[a
]))
2633 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2636 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2637 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2639 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2640 ? FILTER_FLAG_OP_ANY
2641 : FILTER_FLAG_OP_ALL
;
2645 cli_error(cli
, "Specify tcp flags");
2649 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2653 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2655 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2656 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2657 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2658 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2659 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2660 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2663 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2672 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2674 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2676 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2686 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2693 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2695 static ip_filter_rulet rule
;
2696 struct in_addr addr
;
2698 if (CLI_HELP_REQUESTED
)
2702 cli_arg_help(cli
, argv
[0][1],
2703 "A.B.C.D", "Source address",
2704 "any", "Any source address",
2705 "host", "Source host",
2711 if (MATCH("any", argv
[0]))
2713 if (argc
== 2 && !argv
[1][1])
2714 cli_arg_help(cli
, 1, NULL
);
2716 else if (MATCH("host", argv
[0]))
2720 cli_arg_help(cli
, argv
[1][1],
2721 "A.B.C.D", "Host address",
2724 else if (argc
== 3 && !argv
[2][1])
2725 cli_arg_help(cli
, 1, NULL
);
2731 cli_arg_help(cli
, 1,
2732 "A.B.C.D", "Wildcard bits",
2735 else if (argc
== 3 && !argv
[2][1])
2736 cli_arg_help(cli
, 1, NULL
);
2744 cli_error(cli
, "Specify rule details");
2748 memset(&rule
, 0, sizeof(rule
));
2749 rule
.action
= (command
[0] == 'p')
2750 ? FILTER_ACTION_PERMIT
2751 : FILTER_ACTION_DENY
;
2753 rule
.proto
= IPPROTO_IP
;
2754 if (MATCH("any", argv
[0]))
2756 rule
.src_ip
= INADDR_ANY
;
2757 rule
.src_wild
= INADDR_BROADCAST
;
2759 else if (MATCH("host", argv
[0]))
2763 cli_error(cli
, "Specify host ip address");
2767 if (!inet_aton(argv
[1], &addr
))
2769 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2773 rule
.src_ip
= addr
.s_addr
;
2774 rule
.src_wild
= INADDR_ANY
;
2780 cli_error(cli
, "Specify source ip address and wildcard");
2784 if (!inet_aton(argv
[0], &addr
))
2786 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2790 rule
.src_ip
= addr
.s_addr
;
2794 if (!inet_aton(argv
[1], &addr
))
2796 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2800 rule
.src_wild
= addr
.s_addr
;
2803 rule
.src_wild
= INADDR_ANY
;
2809 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2812 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2813 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2814 : access_list_rule_std(cli
, command
, argv
, argc
);
2819 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2821 if (!ip_filters
[filt
].rules
[i
].action
)
2823 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2827 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2831 cli_error(cli
, "Too many rules");
2835 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2840 /* filter USER {in|out} FILTER ... */
2841 if (CLI_HELP_REQUESTED
)
2846 return cli_arg_help(cli
, 0,
2847 "USER", "Username of session to filter", NULL
);
2851 return cli_arg_help(cli
, 0,
2852 "in", "Set incoming filter",
2853 "out", "Set outgoing filter", NULL
);
2857 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2858 "NAME", "Filter name", NULL
);
2861 return cli_arg_help(cli
, argc
> 1, NULL
);
2865 if (!config
->cluster_iam_master
)
2867 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2868 fmtaddr(config
->cluster_master_address
, 0));
2873 if (argc
!= 3 && argc
!= 5)
2875 cli_error(cli
, "Specify a user and filters");
2879 if (!(s
= sessionbyuser(argv
[0])))
2881 cli_error(cli
, "User %s is not connected", argv
[0]);
2885 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2886 for (i
= 1; i
< argc
; i
+= 2)
2891 if (MATCH("in", argv
[i
]))
2893 if (session
[s
].filter_in
)
2895 cli_error(cli
, "Input already filtered");
2898 f
= &cli_session_actions
[s
].filter_in
;
2900 else if (MATCH("out", argv
[i
]))
2902 if (session
[s
].filter_out
)
2904 cli_error(cli
, "Output already filtered");
2907 f
= &cli_session_actions
[s
].filter_out
;
2911 cli_error(cli
, "Invalid filter specification");
2915 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
2916 if (v
< 0 || !*ip_filters
[v
].name
)
2918 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
2925 cli_print(cli
, "Filtering user %s", argv
[0]);
2926 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
2931 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2936 if (CLI_HELP_REQUESTED
)
2937 return cli_arg_help(cli
, argc
> 1,
2938 "USER", "Username of session to remove filters from", NULL
);
2940 if (!config
->cluster_iam_master
)
2942 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2943 fmtaddr(config
->cluster_master_address
, 0));
2950 cli_error(cli
, "Specify a user to remove filters from");
2954 for (i
= 0; i
< argc
; i
++)
2956 if (!(s
= sessionbyuser(argv
[i
])))
2958 cli_error(cli
, "User %s is not connected", argv
[i
]);
2962 if (session
[s
].filter_in
|| session
[s
].filter_out
)
2964 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
2965 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
2969 cli_error(cli
, "User %s not filtered", argv
[i
]);
2976 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2980 if (CLI_HELP_REQUESTED
)
2981 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
2985 cli_error(cli
, "Specify a filter name");
2989 for (i
= 0; i
< argc
; i
++)
2991 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
2992 ip_filter_rulet
*rules
;
2994 if (f
< 0 || !*ip_filters
[f
].name
)
2996 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3003 cli_print(cli
, "%s IP access list %s",
3004 ip_filters
[f
].extended
? "Extended" : "Standard",
3005 ip_filters
[f
].name
);
3007 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3009 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3011 cli_print(cli
, "%s (%u match%s)", r
,
3012 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3014 cli_print(cli
, "%s", r
);
3021 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3023 if (CLI_HELP_REQUESTED
)
3024 return CLI_HELP_NO_ARGS
;
3026 kill(getppid(), SIGQUIT
);
3030 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3032 if (CLI_HELP_REQUESTED
)
3033 return CLI_HELP_NO_ARGS
;
3035 kill(getppid(), SIGHUP
);