1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.76 2006/12/18 12:08:28 bodea Exp $";
19 #include <arpa/inet.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
29 #include "constants.h"
38 extern tunnelt
*tunnel
;
39 extern bundlet
*bundle
;
40 extern sessiont
*session
;
41 extern radiust
*radius
;
42 extern ippoolt
*ip_address_pool
;
43 extern struct Tstats
*_statistics
;
44 static struct cli_def
*cli
= NULL
;
45 extern configt
*config
;
46 extern config_descriptt config_values
[];
48 extern struct Tringbuffer
*ringbuffer
;
50 extern struct cli_session_actions
*cli_session_actions
;
51 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
52 extern tbft
*filter_list
;
53 extern ip_filtert
*ip_filters
;
67 static int debug_rb_tail
;
68 static char *debug_levels
[] = {
79 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
102 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
103 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
104 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
107 static int regular_stuff(struct cli_def
*cli
);
110 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
111 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
112 #endif /* STATISTICS */
115 #define MODE_CONFIG_BGP 8
116 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
120 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
121 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
122 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
125 #define MODE_CONFIG_NACL 9
126 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
127 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
128 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
129 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
130 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
131 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
133 /* match if b is a substr of a */
134 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
136 void init_cli(char *hostname
)
140 struct cli_command
*c
;
141 struct cli_command
*c2
;
143 struct sockaddr_in addr
;
146 if (hostname
&& *hostname
)
147 cli_set_hostname(cli
, hostname
);
149 cli_set_hostname(cli
, "l2tpns");
151 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
152 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
154 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
156 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
157 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
158 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
159 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
160 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
161 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
162 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
163 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
164 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
165 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
166 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");
167 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
168 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
170 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
171 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
172 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
175 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
177 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
178 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
181 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
182 cli_register_command(cli
, NULL
, "shutdown", cmd_shutdown
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Shutdown l2tpns daemon and exit");
183 cli_register_command(cli
, NULL
, "reload", cmd_reload
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Reload configuration");
185 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
186 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
187 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
189 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
190 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
191 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
192 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
195 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
196 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
199 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
200 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
201 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
202 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
203 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
206 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
207 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
209 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
210 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
212 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
213 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
215 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
217 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
218 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
221 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
222 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
223 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
224 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
226 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
227 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
229 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
230 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
232 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
234 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
235 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
237 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
238 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
240 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
241 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
242 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
244 // Enable regular processing
245 cli_regular(cli
, regular_stuff
);
247 if (!(f
= fopen(CLIUSERS
, "r")))
249 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
253 while (fgets(buf
, 4096, f
))
256 if (*buf
== '#') continue;
257 if ((p
= strchr(buf
, '\r'))) *p
= 0;
258 if ((p
= strchr(buf
, '\n'))) *p
= 0;
260 if (!(p
= strchr((char *)buf
, ':'))) continue;
262 if (!strcmp(buf
, "enable"))
264 cli_allow_enable(cli
, p
);
265 LOG(3, 0, 0, "Setting enable password\n");
269 cli_allow_user(cli
, buf
, p
);
270 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
276 memset(&addr
, 0, sizeof(addr
));
277 clifd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
278 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
281 // Set cli fd as non-blocking
282 flags
= fcntl(clifd
, F_GETFL
, 0);
283 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
285 addr
.sin_family
= AF_INET
;
286 addr
.sin_addr
.s_addr
= config
->cli_bind_address
; /* defaults to INADDR_ANY */
287 addr
.sin_port
= htons(23);
288 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
290 LOG(0, 0, 0, "Error binding cli on port 23: %s\n", strerror(errno
));
295 if (listen(clifd
, 10) < 0)
297 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
304 void cli_do(int sockfd
)
306 int require_auth
= 1;
307 struct sockaddr_in addr
;
308 socklen_t l
= sizeof(addr
);
310 if (fork_and_close()) return;
311 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
313 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
314 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
315 fmtaddr(addr
.sin_addr
.s_addr
, 0));
318 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
322 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
323 if (!cli
->users
) /* paranoia */
325 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
331 /* no username/pass required */
336 debug_rb_tail
= ringbuffer
->tail
;
338 memset(&debug_flags
, 0, sizeof(debug_flags
));
339 debug_flags
.critical
= 1;
341 cli_loop(cli
, sockfd
);
344 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
345 fmtaddr(addr
.sin_addr
.s_addr
, 0));
350 static void cli_print_log(struct cli_def
*cli
, char *string
)
352 LOG(3, 0, 0, "%s\n", string
);
355 void cli_do_file(FILE *fh
)
357 LOG(3, 0, 0, "Reading configuration file\n");
358 cli_print_callback(cli
, cli_print_log
);
359 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
360 cli_print_callback(cli
, NULL
);
363 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
374 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
376 int v
= va_arg(ap
, int);
377 snprintf(buf
, sizeof(buf
), entry
, v
);
383 desc
= va_arg(ap
, char *);
385 cli_error(cli
, " %-20s %s", p
, desc
);
387 cli_error(cli
, " %s", p
);
389 entry
= desc
? va_arg(ap
, char *) : 0;
394 cli_error(cli
, " <cr>");
399 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
403 if (CLI_HELP_REQUESTED
)
404 return cli_arg_help(cli
, 1,
405 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
411 // Show individual session
412 for (i
= 0; i
< argc
; i
++)
414 unsigned int s
, b_in
, b_out
;
416 if (s
<= 0 || s
>= MAXSESSION
)
418 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
421 cli_print(cli
, "\r\nSession %d:", s
);
422 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
423 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
424 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
425 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
426 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
427 switch (session
[s
].ppp
.phase
)
430 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
435 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
436 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
437 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
439 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
440 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
441 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
442 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
443 if (session
[s
].session_timeout
)
445 clockt opened
= session
[s
].opened
;
446 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
447 opened
= bundle
[session
[s
].bundle
].online_time
;
449 cli_print(cli
, "\tSess Timeout:\t%u seconds", session
[s
].session_timeout
- (opened
? abs(time_now
- opened
) : 0));
452 if (session
[s
].idle_timeout
)
453 cli_print(cli
, "\tIdle Timeout:\t%u seconds", session
[s
].idle_timeout
- (session
[s
].last_data
? abs(time_now
- session
[s
].last_data
) : 0));
455 if (session
[s
].timeout
)
457 cli_print(cli
, "\tRemaining time:\t%u",
458 (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
459 ? (unsigned) (session
[s
].timeout
- bundle
[session
[s
].bundle
].online_time
)
460 : (unsigned) (session
[s
].timeout
- (time_now
- session
[s
].opened
)));
463 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
464 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
465 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
466 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
467 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
468 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
469 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
470 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
471 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
472 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
473 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
475 cli_print(cli
, "\tIntercepted:\tno");
477 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
479 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
480 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
481 t
? "YES" : "no", t
? " (" : "",
482 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
484 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
488 b_in
= session
[s
].tbf_in
;
489 b_out
= session
[s
].tbf_out
;
491 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
492 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
493 "ByteSent", "PackSent", "PackDrop", "PackDelay");
496 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
498 (filter_list
[b_in
].next
? "*" : " "),
499 (b_in
< 100 ? "\t" : ""),
500 filter_list
[b_in
].rate
* 8,
501 filter_list
[b_in
].credit
,
502 filter_list
[b_in
].queued
,
503 filter_list
[b_in
].b_queued
,
504 filter_list
[b_in
].p_queued
,
505 filter_list
[b_in
].b_sent
,
506 filter_list
[b_in
].p_sent
,
507 filter_list
[b_in
].p_dropped
,
508 filter_list
[b_in
].p_delayed
);
511 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
513 (filter_list
[b_out
].next
? "*" : " "),
514 (b_out
< 100 ? "\t" : ""),
515 filter_list
[b_out
].rate
* 8,
516 filter_list
[b_out
].credit
,
517 filter_list
[b_out
].queued
,
518 filter_list
[b_out
].b_queued
,
519 filter_list
[b_out
].p_queued
,
520 filter_list
[b_out
].b_sent
,
521 filter_list
[b_out
].p_sent
,
522 filter_list
[b_out
].p_dropped
,
523 filter_list
[b_out
].p_delayed
);
530 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-15s %s",
547 for (i
= 1; i
< MAXSESSION
; i
++)
550 if (!session
[i
].opened
) continue;
551 if (session
[i
].bundle
&& bundle
[session
[i
].bundle
].num_of_links
> 1)
552 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- bundle
[session
[i
].bundle
].online_time
) : 0;
554 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- (time_now
-session
[i
].opened
)) : 0;
556 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %-15s %s",
559 session
[i
].user
[0] ? session
[i
].user
: "*",
560 fmtaddr(htonl(session
[i
].ip
), 0),
561 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
562 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
563 (session
[i
].walled_garden
) ? "Y" : "N",
564 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
565 abs(time_now
- (unsigned long)session
[i
].opened
),
566 (unsigned long)session
[i
].cout
,
567 (unsigned long)session
[i
].cin
,
568 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
569 (unsigned long)(rem_time
),
570 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
571 session
[i
].calling
[0] ? session
[i
].calling
: "*");
576 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
578 int i
, x
, show_all
= 0;
586 if (CLI_HELP_REQUESTED
)
589 return cli_arg_help(cli
, 1,
590 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
593 return cli_arg_help(cli
, 1,
594 "all", "Show all tunnels, including unused",
595 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
602 if (strcmp(argv
[0], "all") == 0)
608 // Show individual tunnel
609 for (i
= 0; i
< argc
; i
++)
614 if (t
<= 0 || t
>= MAXTUNNEL
)
616 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
619 cli_print(cli
, "\r\nTunnel %d:", t
);
620 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
621 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
622 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
623 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
624 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
625 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
626 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
627 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
628 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
630 for (x
= 0; x
< MAXSESSION
; x
++)
631 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
632 sprintf(s
, "%s%u ", s
, x
);
634 cli_print(cli
, "\tSessions:\t%s", s
);
640 // Show tunnel summary
641 cli_print(cli
, "%4s %20s %20s %6s %s",
648 for (i
= 1; i
< MAXTUNNEL
; i
++)
651 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
653 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
654 cli_print(cli
, "%4d %20s %20s %6s %6d",
656 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
657 fmtaddr(htonl(tunnel
[i
].ip
), 0),
658 states
[tunnel
[i
].state
],
665 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
672 if (CLI_HELP_REQUESTED
)
673 return cli_arg_help(cli
, 1,
674 "USER", "Show details for specific username",
677 for (i
= 0; i
< MAXSESSION
; i
++)
679 if (!session
[i
].opened
) continue;
680 if (!session
[i
].user
[0]) continue;
684 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
686 if (strcmp(argv
[j
], session
[i
].user
) == 0)
688 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
689 sargv
[sargc
] = sid
[sargc
];
697 cli_print(cli
, "%s", session
[i
].user
);
701 return cmd_show_session(cli
, "users", sargv
, sargc
);
707 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
709 if (CLI_HELP_REQUESTED
)
710 return CLI_HELP_NO_ARGS
;
712 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
713 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
714 GET_STAT(tun_rx_bytes
),
715 GET_STAT(tun_rx_packets
),
716 GET_STAT(tun_rx_errors
),
717 GET_STAT(tun_rx_dropped
));
718 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
719 GET_STAT(tun_tx_bytes
),
720 GET_STAT(tun_tx_packets
),
721 GET_STAT(tun_tx_errors
));
724 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
725 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
726 GET_STAT(tunnel_rx_bytes
),
727 GET_STAT(tunnel_rx_packets
),
728 GET_STAT(tunnel_rx_errors
));
729 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
730 GET_STAT(tunnel_tx_bytes
),
731 GET_STAT(tunnel_tx_packets
),
732 GET_STAT(tunnel_tx_errors
),
733 GET_STAT(tunnel_retries
));
736 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
737 cli_print(cli
, "-----------------------------------------");
738 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
739 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
740 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
741 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
742 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
743 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
744 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
745 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
746 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
747 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
748 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
749 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
750 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
751 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
752 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
753 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
754 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
755 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
759 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
760 cli_print(cli
, "-----------------------------------------");
761 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
762 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
763 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
764 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
765 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
766 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
767 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
768 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
769 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
770 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
771 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
772 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
773 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
774 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
775 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
776 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
777 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
778 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
779 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
780 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
781 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
782 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
783 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
784 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
785 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
786 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
787 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
788 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
789 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
790 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
791 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
792 #endif /* STAT_CALLS */
795 time_t l
= GET_STAT(last_reset
);
797 char *p
= strchr(t
, '\n');
801 cli_print(cli
, "Last counter reset %s", t
);
807 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
809 if (CLI_HELP_REQUESTED
)
810 return CLI_HELP_NO_ARGS
;
812 memset(_statistics
, 0, sizeof(struct Tstats
));
813 SET_STAT(last_reset
, time(NULL
));
815 cli_print(cli
, "Counters cleared");
818 #endif /* STATISTICS */
820 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
826 if (CLI_HELP_REQUESTED
)
827 return cli_arg_help(cli
, 1,
828 "tag", "Include CVS release tag",
829 "file", "Include file versions",
832 for (i
= 0; i
< argc
; i
++)
833 if (!strcmp(argv
[i
], "tag"))
835 else if (!strcmp(argv
[i
], "file"))
838 cli_print(cli
, "L2TPNS %s", VERSION
);
841 char const *p
= strchr(cvs_name
, ':');
853 e
= strpbrk(p
, " \t$");
854 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
859 extern linked_list
*loaded_plugins
;
862 cli_print(cli
, "Files:");
863 cli_print(cli
, " %s", cvs_id_arp
);
865 cli_print(cli
, " %s", cvs_id_bgp
);
867 cli_print(cli
, " %s", cvs_id_cli
);
868 cli_print(cli
, " %s", cvs_id_cluster
);
869 cli_print(cli
, " %s", cvs_id_constants
);
870 cli_print(cli
, " %s", cvs_id_control
);
871 cli_print(cli
, " %s", cvs_id_icmp
);
872 cli_print(cli
, " %s", cvs_id_l2tpns
);
873 cli_print(cli
, " %s", cvs_id_ll
);
874 cli_print(cli
, " %s", cvs_id_ppp
);
875 cli_print(cli
, " %s", cvs_id_radius
);
876 cli_print(cli
, " %s", cvs_id_tbf
);
877 cli_print(cli
, " %s", cvs_id_util
);
879 ll_reset(loaded_plugins
);
880 while ((p
= ll_next(loaded_plugins
)))
882 char const **id
= dlsym(p
, "cvs_id");
884 cli_print(cli
, " %s", *id
);
891 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
894 int used
= 0, free
= 0, show_all
= 0;
896 if (!config
->cluster_iam_master
)
898 cli_print(cli
, "Can't do this on a slave. Do it on %s",
899 fmtaddr(config
->cluster_master_address
, 0));
904 if (CLI_HELP_REQUESTED
)
907 return cli_arg_help(cli
, 1, NULL
);
909 return cli_arg_help(cli
, 1,
910 "all", "Show all pool addresses, including unused",
914 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
918 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
919 for (i
= 0; i
< MAXIPPOOL
; i
++)
921 if (!ip_address_pool
[i
].address
) continue;
922 if (ip_address_pool
[i
].assigned
)
924 cli_print(cli
, "%-15s\tY %8d %s",
925 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
926 ip_address_pool
[i
].session
,
927 session
[ip_address_pool
[i
].session
].user
);
933 if (ip_address_pool
[i
].last
)
934 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
935 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
936 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
939 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
946 cli_print(cli
, "(Not displaying unused addresses)");
948 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
952 static FILE *save_config_fh
= 0;
953 static void print_save_config(struct cli_def
*cli
, char *string
)
956 fprintf(save_config_fh
, "%s\n", string
);
959 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
961 if (CLI_HELP_REQUESTED
)
962 return CLI_HELP_NO_ARGS
;
964 if ((save_config_fh
= fopen(config
->config_file
, "w")))
966 cli_print(cli
, "Writing configuration");
967 cli_print_callback(cli
, print_save_config
);
968 cmd_show_run(cli
, command
, argv
, argc
);
969 cli_print_callback(cli
, NULL
);
970 fclose(save_config_fh
);
975 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
980 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
982 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
985 char ipv6addr
[INET6_ADDRSTRLEN
];
987 if (CLI_HELP_REQUESTED
)
988 return CLI_HELP_NO_ARGS
;
990 cli_print(cli
, "# Current configuration:");
992 for (i
= 0; config_values
[i
].key
; i
++)
994 void *value
= ((void *)config
) + config_values
[i
].offset
;
995 if (config_values
[i
].type
== STRING
)
996 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
997 else if (config_values
[i
].type
== IPv4
)
998 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
999 else if (config_values
[i
].type
== IPv6
)
1000 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
1001 else if (config_values
[i
].type
== SHORT
)
1002 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
1003 else if (config_values
[i
].type
== BOOL
)
1004 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
1005 else if (config_values
[i
].type
== INT
)
1006 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
1007 else if (config_values
[i
].type
== UNSIGNED_LONG
)
1008 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
1011 cli_print(cli
, "# Plugins");
1012 for (i
= 0; i
< MAXPLUGINS
; i
++)
1014 if (*config
->plugins
[i
])
1016 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
1021 if (config
->as_number
)
1026 cli_print(cli
, "# BGP");
1027 cli_print(cli
, "router bgp %u", config
->as_number
);
1028 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1030 if (!config
->neighbour
[i
].name
[0])
1033 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
1035 k
= config
->neighbour
[i
].keepalive
;
1036 h
= config
->neighbour
[i
].hold
;
1043 k
= BGP_KEEPALIVE_TIME
;
1049 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1051 if (config
->neighbour
[i
].update_source
.s_addr
!= INADDR_ANY
)
1052 cli_print(cli
, " neighbour %s update-source %s",
1053 config
->neighbour
[i
].name
,
1054 inet_ntoa(config
->neighbour
[i
].update_source
));
1059 cli_print(cli
, "# Filters");
1060 for (i
= 0; i
< MAXFILTER
; i
++)
1062 ip_filter_rulet
*rules
;
1063 if (!*ip_filters
[i
].name
)
1066 cli_print(cli
, "ip access-list %s %s",
1067 ip_filters
[i
].extended
? "extended" : "standard",
1068 ip_filters
[i
].name
);
1070 rules
= ip_filters
[i
].rules
;
1071 while (rules
->action
)
1072 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1075 cli_print(cli
, "# end");
1079 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1081 int i
, free
= 0, used
= 0, show_all
= 0;
1093 if (CLI_HELP_REQUESTED
)
1096 return cli_arg_help(cli
, 1, NULL
);
1098 return cli_arg_help(cli
, 1,
1099 "all", "Show all RADIUS sessions, including unused",
1103 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1107 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1110 for (i
= 1; i
< MAXRADIUS
; i
++)
1112 if (radius
[i
].state
== RADIUSNULL
)
1117 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1119 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1123 states
[radius
[i
].state
],
1129 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1134 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1138 if (CLI_HELP_REQUESTED
)
1139 return CLI_HELP_NO_ARGS
;
1141 cli_print(cli
, "Plugins currently loaded:");
1142 for (i
= 0; i
< MAXPLUGINS
; i
++)
1143 if (*config
->plugins
[i
])
1144 cli_print(cli
, " %s", config
->plugins
[i
]);
1149 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1153 if (CLI_HELP_REQUESTED
)
1154 return CLI_HELP_NO_ARGS
;
1156 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1165 for (i
= 0; i
< MAXSESSION
; i
++)
1167 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1168 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1172 session
[i
].throttle_in
,
1173 session
[i
].throttle_out
,
1175 session
[i
].tbf_out
);
1181 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1183 if (CLI_HELP_REQUESTED
)
1184 return CLI_HELP_NO_ARGS
;
1186 cli_print(cli
, " _\n"
1192 " ( \\ `. `-. _,.-:\\\n"
1193 " \\ \\ `. `-._ __..--' ,-';/\n"
1194 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1195 " `. `. `-._ __..--' ,' /\n"
1196 " `. `-_ ``--..'' _.-' ,'\n"
1197 " `-_ `-.___ __,--' ,'\n"
1198 " `-.__ `----\"\"\" __.-'\n"
1199 "hh `--..____..--'");
1204 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1209 if (CLI_HELP_REQUESTED
)
1210 return cli_arg_help(cli
, argc
> 1,
1211 "USER", "Username of session to drop", NULL
);
1213 if (!config
->cluster_iam_master
)
1215 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1216 fmtaddr(config
->cluster_master_address
, 0));
1223 cli_error(cli
, "Specify a user to drop");
1227 for (i
= 0; i
< argc
; i
++)
1229 if (!(s
= sessionbyuser(argv
[i
])))
1231 cli_error(cli
, "User %s is not connected", argv
[i
]);
1235 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1237 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1238 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1245 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1250 if (CLI_HELP_REQUESTED
)
1251 return cli_arg_help(cli
, argc
> 1,
1252 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1254 if (!config
->cluster_iam_master
)
1256 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1257 fmtaddr(config
->cluster_master_address
, 0));
1264 cli_error(cli
, "Specify a tunnel to drop");
1268 for (i
= 0; i
< argc
; i
++)
1270 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1272 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1278 cli_error(cli
, "Tunnel %d is not connected", t
);
1284 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1288 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1289 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1295 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1300 if (CLI_HELP_REQUESTED
)
1301 return cli_arg_help(cli
, argc
> 1,
1302 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1304 if (!config
->cluster_iam_master
)
1306 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1307 fmtaddr(config
->cluster_master_address
, 0));
1314 cli_error(cli
, "Specify a session id to drop");
1318 for (i
= 0; i
< argc
; i
++)
1320 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1322 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1326 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1328 cli_print(cli
, "Dropping session %d", s
);
1329 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1333 cli_error(cli
, "Session %d is not active.", s
);
1340 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1346 if (CLI_HELP_REQUESTED
)
1351 return cli_arg_help(cli
, 0,
1352 "USER", "Username of session to snoop", NULL
);
1355 return cli_arg_help(cli
, 0,
1356 "A.B.C.D", "IP address of snoop destination", NULL
);
1359 return cli_arg_help(cli
, 0,
1360 "N", "Port of snoop destination", NULL
);
1364 return cli_arg_help(cli
, 1, NULL
);
1371 if (!config
->cluster_iam_master
)
1373 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1374 fmtaddr(config
->cluster_master_address
, 0));
1381 cli_error(cli
, "Specify username, ip and port");
1385 if (!(s
= sessionbyuser(argv
[0])))
1387 cli_error(cli
, "User %s is not connected", argv
[0]);
1391 ip
= inet_addr(argv
[1]);
1392 if (!ip
|| ip
== INADDR_NONE
)
1394 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1398 port
= atoi(argv
[2]);
1401 cli_error(cli
, "Invalid port %s", argv
[2]);
1405 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1406 cli_session_actions
[s
].snoop_ip
= ip
;
1407 cli_session_actions
[s
].snoop_port
= port
;
1408 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1413 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1418 if (CLI_HELP_REQUESTED
)
1419 return cli_arg_help(cli
, argc
> 1,
1420 "USER", "Username of session to unsnoop", NULL
);
1422 if (!config
->cluster_iam_master
)
1424 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1425 fmtaddr(config
->cluster_master_address
, 0));
1432 cli_error(cli
, "Specify a user to unsnoop");
1436 for (i
= 0; i
< argc
; i
++)
1438 if (!(s
= sessionbyuser(argv
[i
])))
1440 cli_error(cli
, "User %s is not connected", argv
[i
]);
1444 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1445 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1451 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1458 throttle USER - throttle in/out to default rate
1459 throttle USER RATE - throttle in/out to default rate
1460 throttle USER in RATE - throttle input only
1461 throttle USER out RATE - throttle output only
1462 throttle USER in RATE out RATE - throttle both
1465 if (CLI_HELP_REQUESTED
)
1470 return cli_arg_help(cli
, 0,
1471 "USER", "Username of session to throttle", NULL
);
1474 return cli_arg_help(cli
, 1,
1475 "RATE", "Rate in kbps (in and out)",
1476 "in", "Select incoming rate",
1477 "out", "Select outgoing rate", NULL
);
1480 return cli_arg_help(cli
, 1,
1481 "in", "Select incoming rate",
1482 "out", "Select outgoing rate", NULL
);
1485 if (isdigit(argv
[1][0]))
1486 return cli_arg_help(cli
, 1, NULL
);
1489 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1492 return cli_arg_help(cli
, argc
> 1, NULL
);
1496 if (!config
->cluster_iam_master
)
1498 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1499 fmtaddr(config
->cluster_master_address
, 0));
1506 cli_error(cli
, "Specify a user to throttle");
1510 if (!(s
= sessionbyuser(argv
[0])))
1512 cli_error(cli
, "User %s is not connected", argv
[0]);
1518 rate_in
= rate_out
= config
->rl_rate
;
1522 rate_in
= rate_out
= atoi(argv
[1]);
1525 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1529 else if (argc
== 3 || argc
== 5)
1532 for (i
= 1; i
< argc
- 1; i
+= 2)
1535 if (MATCH("in", argv
[i
]))
1536 r
= rate_in
= atoi(argv
[i
+1]);
1537 else if (MATCH("out", argv
[i
]))
1538 r
= rate_out
= atoi(argv
[i
+1]);
1542 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1549 cli_error(cli
, "Invalid arguments");
1553 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1555 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1559 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1560 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1561 cli_session_actions
[s
].throttle_in
= rate_in
;
1563 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1564 cli_session_actions
[s
].throttle_out
= rate_out
;
1566 if (cli_session_actions
[s
].throttle_in
== -1 &&
1567 cli_session_actions
[s
].throttle_out
== -1)
1569 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1573 cli_print(cli
, "Throttling user %s", argv
[0]);
1574 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1579 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1584 if (CLI_HELP_REQUESTED
)
1585 return cli_arg_help(cli
, argc
> 1,
1586 "USER", "Username of session to unthrottle", NULL
);
1588 if (!config
->cluster_iam_master
)
1590 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1591 fmtaddr(config
->cluster_master_address
, 0));
1598 cli_error(cli
, "Specify a user to unthrottle");
1602 for (i
= 0; i
< argc
; i
++)
1604 if (!(s
= sessionbyuser(argv
[i
])))
1606 cli_error(cli
, "User %s is not connected", argv
[i
]);
1610 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1612 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1613 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1617 cli_error(cli
, "User %s not throttled", argv
[i
]);
1624 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1628 if (CLI_HELP_REQUESTED
)
1629 return cli_arg_help(cli
, 1,
1630 "all", "Enable debugging for all except \"data\"",
1631 "critical", "", // FIXME: add descriptions
1641 char *p
= (char *) &debug_flags
;
1642 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1646 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1647 (debug_flags
.critical
) ? " critical" : "",
1648 (debug_flags
.error
) ? " error" : "",
1649 (debug_flags
.warning
) ? " warning" : "",
1650 (debug_flags
.info
) ? " info" : "",
1651 (debug_flags
.calls
) ? " calls" : "",
1652 (debug_flags
.data
) ? " data" : "");
1658 cli_print(cli
, "Debugging off");
1662 for (i
= 0; i
< argc
; i
++)
1664 int len
= strlen(argv
[i
]);
1666 if (argv
[i
][0] == 'c' && len
< 2)
1667 len
= 2; /* distinguish [cr]itical from [ca]lls */
1669 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1670 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1671 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1672 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1673 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1674 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1675 if (!strncmp("all", argv
[i
], len
))
1677 memset(&debug_flags
, 1, sizeof(debug_flags
));
1678 debug_flags
.data
= 0;
1682 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1688 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1692 if (CLI_HELP_REQUESTED
)
1693 return cli_arg_help(cli
, 1,
1694 "all", "Disable all debugging",
1695 "critical", "", // FIXME: add descriptions
1705 memset(&debug_flags
, 0, sizeof(debug_flags
));
1709 for (i
= 0; i
< argc
; i
++)
1711 int len
= strlen(argv
[i
]);
1713 if (argv
[i
][0] == 'c' && len
< 2)
1714 len
= 2; /* distinguish [cr]itical from [ca]lls */
1716 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1717 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1718 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1719 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1720 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1721 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1722 if (!strncmp("all", argv
[i
], len
))
1724 memset(&debug_flags
, 0, sizeof(debug_flags
));
1728 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1734 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1736 int i
, firstfree
= 0;
1738 if (CLI_HELP_REQUESTED
)
1739 return cli_arg_help(cli
, argc
> 1,
1740 "PLUGIN", "Name of plugin to load", NULL
);
1744 cli_error(cli
, "Specify a plugin to load");
1748 for (i
= 0; i
< MAXPLUGINS
; i
++)
1750 if (!*config
->plugins
[i
] && !firstfree
)
1752 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1754 cli_error(cli
, "Plugin is already loaded");
1761 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1762 config
->reload_config
= 1;
1763 cli_print(cli
, "Loading plugin %s", argv
[0]);
1769 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1773 if (CLI_HELP_REQUESTED
)
1774 return cli_arg_help(cli
, argc
> 1,
1775 "PLUGIN", "Name of plugin to unload", NULL
);
1779 cli_error(cli
, "Specify a plugin to remove");
1783 for (i
= 0; i
< MAXPLUGINS
; i
++)
1785 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1787 config
->reload_config
= 1;
1788 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1793 cli_error(cli
, "Plugin is not loaded");
1797 static char *duration(time_t secs
)
1799 static char *buf
= NULL
;
1802 if (!buf
) buf
= calloc(64, 1);
1806 int days
= secs
/ 86400;
1807 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1813 int mins
= secs
/ 60;
1814 int hrs
= mins
/ 60;
1817 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1819 else if (secs
>= 60)
1821 int mins
= secs
/ 60;
1822 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1825 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1830 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1833 char buf
[100], *p
= buf
, *loads
[3];
1834 int i
, num_sessions
= 0;
1836 if (CLI_HELP_REQUESTED
)
1837 return CLI_HELP_NO_ARGS
;
1839 fh
= fopen("/proc/loadavg", "r");
1840 fgets(buf
, 100, fh
);
1843 for (i
= 0; i
< 3; i
++)
1844 loads
[i
] = strdup(strsep(&p
, " "));
1847 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1849 for (i
= 1; i
< MAXSESSION
; i
++)
1850 if (session
[i
].opened
) num_sessions
++;
1852 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1854 duration(time_now
- config
->start_time
),
1856 loads
[0], loads
[1], loads
[2]
1858 for (i
= 0; i
< 3; i
++)
1859 if (loads
[i
]) free(loads
[i
]);
1861 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1866 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1870 if (CLI_HELP_REQUESTED
)
1876 int len
= strlen(argv
[0])-1;
1877 for (i
= 0; config_values
[i
].key
; i
++)
1878 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1879 cli_error(cli
, " %s", config_values
[i
].key
);
1885 return cli_arg_help(cli
, 0,
1886 "VALUE", "Value for variable", NULL
);
1890 return cli_arg_help(cli
, 1, NULL
);
1899 cli_error(cli
, "Specify variable and value");
1903 for (i
= 0; config_values
[i
].key
; i
++)
1905 void *value
= ((void *) config
) + config_values
[i
].offset
;
1906 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1908 // Found a value to set
1909 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1910 switch (config_values
[i
].type
)
1913 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1916 *(int *) value
= atoi(argv
[1]);
1919 *(unsigned long *) value
= atol(argv
[1]);
1922 *(short *) value
= atoi(argv
[1]);
1925 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1928 inet_pton(AF_INET6
, argv
[1], value
);
1931 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1937 cli_error(cli
, "Unknown variable type");
1940 config
->reload_config
= 1;
1945 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1949 int regular_stuff(struct cli_def
*cli
)
1955 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1957 char *m
= ringbuffer
->buffer
[i
].message
;
1963 switch (ringbuffer
->buffer
[i
].level
)
1965 case 0: show
= debug_flags
.critical
; break;
1966 case 1: show
= debug_flags
.error
; break;
1967 case 2: show
= debug_flags
.warning
; break;
1968 case 3: show
= debug_flags
.info
; break;
1969 case 4: show
= debug_flags
.calls
; break;
1970 case 5: show
= debug_flags
.data
; break;
1973 if (!show
) continue;
1975 if (!(p
= strchr(m
, '\n')))
1978 cli_error(cli
, "\r%s-%u-%u %.*s",
1979 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1980 ringbuffer
->buffer
[i
].tunnel
,
1981 ringbuffer
->buffer
[i
].session
,
1987 debug_rb_tail
= ringbuffer
->tail
;
1995 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1999 if (CLI_HELP_REQUESTED
)
2000 return cli_arg_help(cli
, argc
> 1,
2001 "<1-65535>", "Autonomous system number", NULL
);
2003 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
2005 cli_error(cli
, "Invalid autonomous system number");
2009 if (bgp_configured
&& as
!= config
->as_number
)
2011 cli_error(cli
, "Can't change local AS on a running system");
2015 config
->as_number
= as
;
2016 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
2021 static int find_bgp_neighbour(char const *name
)
2026 in_addr_t addrs
[4] = { 0 };
2029 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
2032 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
2033 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
2035 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2037 if (!config
->neighbour
[i
].name
[0])
2039 if (new == -1) new = i
;
2043 if (!strcmp(name
, config
->neighbour
[i
].name
))
2046 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
2049 for (a
= h
->h_addr_list
; *a
; a
++)
2052 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
2053 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2061 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2067 if (CLI_HELP_REQUESTED
)
2072 return cli_arg_help(cli
, 0,
2073 "A.B.C.D", "BGP neighbour address",
2074 "NAME", "BGP neighbour name",
2078 return cli_arg_help(cli
, 0,
2079 "remote-as", "Set remote autonomous system number",
2080 "timers", "Set timers",
2081 "update-source", "Set source address to use for the BGP session",
2085 if (MATCH("remote-as", argv
[1]))
2086 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2088 if (MATCH("timers", argv
[1]))
2091 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2094 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2096 if (argc
== 5 && !argv
[4][1])
2097 return cli_arg_help(cli
, 1, NULL
);
2100 if (MATCH("update-source", argv
[1]))
2101 return cli_arg_help(cli
, argc
> 3, "A.B.C.D", "Source IP address", NULL
);
2109 cli_error(cli
, "Invalid arguments");
2113 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2115 cli_error(cli
, "Invalid neighbour");
2121 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2125 if (MATCH("remote-as", argv
[1]))
2127 int as
= atoi(argv
[2]);
2128 if (as
< 0 || as
> 65535)
2130 cli_error(cli
, "Invalid autonomous system number");
2134 if (!config
->neighbour
[i
].name
[0])
2136 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2137 config
->neighbour
[i
].keepalive
= -1;
2138 config
->neighbour
[i
].hold
= -1;
2139 config
->neighbour
[i
].update_source
.s_addr
= INADDR_ANY
;
2142 config
->neighbour
[i
].as
= as
;
2146 if (MATCH("update-source", argv
[1]))
2148 struct in_addr addr
;
2150 if (!config
->neighbour
[i
].name
[0])
2152 cli_error(cli
, "Specify remote-as first");
2156 if (!inet_aton(argv
[2], &addr
))
2158 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[2]);
2162 config
->neighbour
[i
].update_source
= addr
;
2166 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2168 cli_error(cli
, "Invalid arguments");
2172 if (!config
->neighbour
[i
].name
[0])
2174 cli_error(cli
, "Specify remote-as first");
2178 keepalive
= atoi(argv
[2]);
2179 hold
= atoi(argv
[3]);
2181 if (keepalive
< 1 || keepalive
> 65535)
2183 cli_error(cli
, "Invalid keepalive time");
2187 if (hold
< 3 || hold
> 65535)
2189 cli_error(cli
, "Invalid hold time");
2193 if (keepalive
== BGP_KEEPALIVE_TIME
)
2194 keepalive
= -1; // using default value
2196 if (hold
== BGP_HOLD_TIME
)
2199 config
->neighbour
[i
].keepalive
= keepalive
;
2200 config
->neighbour
[i
].hold
= hold
;
2205 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2209 if (CLI_HELP_REQUESTED
)
2210 return cli_arg_help(cli
, argc
> 0,
2211 "A.B.C.D", "BGP neighbour address",
2212 "NAME", "BGP neighbour name",
2217 cli_error(cli
, "Specify a BGP neighbour");
2221 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2223 cli_error(cli
, "Invalid neighbour");
2227 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2229 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2233 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2237 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2243 if (!bgp_configured
)
2246 if (CLI_HELP_REQUESTED
)
2247 return cli_arg_help(cli
, 1,
2248 "A.B.C.D", "BGP neighbour address",
2249 "NAME", "BGP neighbour name",
2252 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2253 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2257 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2259 if (!*bgp_peers
[i
].name
)
2262 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2263 if (argc
&& strcmp(addr
, argv
[0]) &&
2264 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2270 cli_print(cli
, "Peer AS Address "
2271 "State Retries Retry in Route Pend Timers");
2272 cli_print(cli
, "------------------ ----- --------------- "
2273 "----------- ------- -------- ----- ---- ---------");
2276 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2280 bgp_state_str(bgp_peers
[i
].state
),
2281 bgp_peers
[i
].retry_count
,
2282 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2283 bgp_peers
[i
].routing
? "yes" : "no",
2284 bgp_peers
[i
].update_routes
? "yes" : "no",
2285 bgp_peers
[i
].keepalive
,
2292 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2297 if (!bgp_configured
)
2300 if (CLI_HELP_REQUESTED
)
2301 return cli_arg_help(cli
, 1,
2302 "A.B.C.D", "BGP neighbour address",
2303 "NAME", "BGP neighbour name",
2306 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2308 if (bgp_peers
[i
].state
!= Established
)
2311 if (!bgp_peers
[i
].routing
)
2314 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2315 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2318 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2319 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2325 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2330 if (!bgp_configured
)
2333 if (CLI_HELP_REQUESTED
)
2334 return cli_arg_help(cli
, 1,
2335 "A.B.C.D", "BGP neighbour address",
2336 "NAME", "BGP neighbour name",
2339 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2341 if (bgp_peers
[i
].state
!= Established
)
2344 if (bgp_peers
[i
].routing
)
2347 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2348 if (argc
&& strcmp(addr
, argv
[0]) &&
2349 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2352 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2353 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2359 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2364 if (!bgp_configured
)
2367 if (CLI_HELP_REQUESTED
)
2368 return cli_arg_help(cli
, 1,
2369 "A.B.C.D", "BGP neighbour address",
2370 "NAME", "BGP neighbour name",
2373 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2375 if (!*bgp_peers
[i
].name
)
2378 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2379 if (argc
&& strcmp(addr
, argv
[0]) &&
2380 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2383 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2384 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2392 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2396 if (CLI_HELP_REQUESTED
)
2401 return cli_arg_help(cli
, 0,
2402 "standard", "Standard syntax",
2403 "extended", "Extended syntax",
2407 return cli_arg_help(cli
, argv
[1][1],
2408 "NAME", "Access-list name",
2412 if (argc
== 3 && !argv
[2][1])
2413 return cli_arg_help(cli
, 1, NULL
);
2421 cli_error(cli
, "Specify access-list type and name");
2425 if (MATCH("standard", argv
[0]))
2427 else if (MATCH("extended", argv
[0]))
2431 cli_error(cli
, "Invalid access-list type");
2435 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2436 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2438 cli_error(cli
, "Invalid access-list name");
2442 filt
= find_filter(argv
[1], strlen(argv
[1]));
2447 cli_error(cli
, "Too many access-lists");
2452 if (!*ip_filters
[filt
].name
)
2454 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2455 strcpy(ip_filters
[filt
].name
, argv
[1]);
2456 ip_filters
[filt
].extended
= extended
;
2458 else if (ip_filters
[filt
].extended
!= extended
)
2460 cli_error(cli
, "Access-list is %s",
2461 ip_filters
[filt
].extended
? "extended" : "standard");
2466 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2470 if (filt
< 0 || !*ip_filters
[filt
].name
)
2472 cli_error(cli
, "Access-list not defined");
2477 if (ip_filters
[filt
].used
)
2479 cli_error(cli
, "Access-list in use");
2483 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2487 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2489 return access_list(cli
, argv
, argc
, 1);
2492 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2494 return access_list(cli
, argv
, argc
, 0);
2497 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2499 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2500 return sprintf(buf
, " any");
2502 if (wild
== INADDR_ANY
)
2503 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2505 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2508 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2512 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2513 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2514 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2515 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2516 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2522 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2524 static char buf
[256];
2527 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2530 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2531 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2534 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2538 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2539 p
+= show_ports(p
, &rule
->src_ports
);
2541 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2542 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2543 p
+= show_ports(p
, &rule
->dst_ports
);
2545 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2547 switch (rule
->tcp_flag_op
)
2549 case FILTER_FLAG_OP_EST
:
2550 p
+= sprintf(p
, " established");
2553 case FILTER_FLAG_OP_ANY
:
2554 case FILTER_FLAG_OP_ALL
:
2555 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2556 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2557 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2558 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2559 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2560 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2561 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2562 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2563 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2564 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2565 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2566 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2567 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2573 p
+= sprintf(p
, " fragments");
2578 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2580 static ip_filter_rulet rule
;
2581 struct in_addr addr
;
2585 if (CLI_HELP_REQUESTED
)
2589 cli_arg_help(cli
, 0,
2590 "ip", "Match IP packets",
2591 "tcp", "Match TCP packets",
2592 "udp", "Match UDP packets",
2598 // *sigh*, too darned complex
2599 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2605 cli_error(cli
, "Specify rule details");
2609 memset(&rule
, 0, sizeof(rule
));
2610 rule
.action
= (command
[0] == 'p')
2611 ? FILTER_ACTION_PERMIT
2612 : FILTER_ACTION_DENY
;
2614 if (MATCH("ip", argv
[0]))
2615 rule
.proto
= IPPROTO_IP
;
2616 else if (MATCH("udp", argv
[0]))
2617 rule
.proto
= IPPROTO_UDP
;
2618 else if (MATCH("tcp", argv
[0]))
2619 rule
.proto
= IPPROTO_TCP
;
2622 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2626 for (a
= 1, i
= 0; i
< 2; i
++)
2630 ip_filter_portt
*port
;
2635 wild
= &rule
.src_wild
;
2636 port
= &rule
.src_ports
;
2641 wild
= &rule
.dst_wild
;
2642 port
= &rule
.dst_ports
;
2645 cli_error(cli
, "Specify destination");
2650 if (MATCH("any", argv
[a
]))
2653 *wild
= INADDR_BROADCAST
;
2656 else if (MATCH("host", argv
[a
]))
2660 cli_error(cli
, "Specify host ip address");
2664 if (!inet_aton(argv
[a
], &addr
))
2666 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2678 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2682 if (!inet_aton(argv
[a
], &addr
))
2684 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2690 if (!inet_aton(argv
[++a
], &addr
))
2692 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2696 *wild
= addr
.s_addr
;
2700 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2704 if (MATCH("eq", argv
[a
]))
2705 port
->op
= FILTER_PORT_OP_EQ
;
2706 else if (MATCH("neq", argv
[a
]))
2707 port
->op
= FILTER_PORT_OP_NEQ
;
2708 else if (MATCH("gt", argv
[a
]))
2709 port
->op
= FILTER_PORT_OP_GT
;
2710 else if (MATCH("lt", argv
[a
]))
2711 port
->op
= FILTER_PORT_OP_LT
;
2712 else if (MATCH("range", argv
[a
]))
2713 port
->op
= FILTER_PORT_OP_RANGE
;
2720 cli_error(cli
, "Specify port");
2724 if (!(port
->port
= atoi(argv
[a
])))
2726 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2731 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2736 cli_error(cli
, "Specify port");
2740 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2742 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2749 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2751 if (MATCH("established", argv
[a
]))
2753 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2756 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2757 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2759 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2760 ? FILTER_FLAG_OP_ANY
2761 : FILTER_FLAG_OP_ALL
;
2765 cli_error(cli
, "Specify tcp flags");
2769 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2773 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2775 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2776 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2777 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2778 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2779 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2780 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2783 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2792 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2794 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2796 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2806 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2813 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2815 static ip_filter_rulet rule
;
2816 struct in_addr addr
;
2818 if (CLI_HELP_REQUESTED
)
2822 cli_arg_help(cli
, argv
[0][1],
2823 "A.B.C.D", "Source address",
2824 "any", "Any source address",
2825 "host", "Source host",
2831 if (MATCH("any", argv
[0]))
2833 if (argc
== 2 && !argv
[1][1])
2834 cli_arg_help(cli
, 1, NULL
);
2836 else if (MATCH("host", argv
[0]))
2840 cli_arg_help(cli
, argv
[1][1],
2841 "A.B.C.D", "Host address",
2844 else if (argc
== 3 && !argv
[2][1])
2845 cli_arg_help(cli
, 1, NULL
);
2851 cli_arg_help(cli
, 1,
2852 "A.B.C.D", "Wildcard bits",
2855 else if (argc
== 3 && !argv
[2][1])
2856 cli_arg_help(cli
, 1, NULL
);
2864 cli_error(cli
, "Specify rule details");
2868 memset(&rule
, 0, sizeof(rule
));
2869 rule
.action
= (command
[0] == 'p')
2870 ? FILTER_ACTION_PERMIT
2871 : FILTER_ACTION_DENY
;
2873 rule
.proto
= IPPROTO_IP
;
2874 if (MATCH("any", argv
[0]))
2876 rule
.src_ip
= INADDR_ANY
;
2877 rule
.src_wild
= INADDR_BROADCAST
;
2879 else if (MATCH("host", argv
[0]))
2883 cli_error(cli
, "Specify host ip address");
2887 if (!inet_aton(argv
[1], &addr
))
2889 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2893 rule
.src_ip
= addr
.s_addr
;
2894 rule
.src_wild
= INADDR_ANY
;
2900 cli_error(cli
, "Specify source ip address and wildcard");
2904 if (!inet_aton(argv
[0], &addr
))
2906 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2910 rule
.src_ip
= addr
.s_addr
;
2914 if (!inet_aton(argv
[1], &addr
))
2916 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2920 rule
.src_wild
= addr
.s_addr
;
2923 rule
.src_wild
= INADDR_ANY
;
2929 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2932 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2933 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2934 : access_list_rule_std(cli
, command
, argv
, argc
);
2939 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2941 if (!ip_filters
[filt
].rules
[i
].action
)
2943 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2947 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2951 cli_error(cli
, "Too many rules");
2955 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2960 /* filter USER {in|out} FILTER ... */
2961 if (CLI_HELP_REQUESTED
)
2966 return cli_arg_help(cli
, 0,
2967 "USER", "Username of session to filter", NULL
);
2971 return cli_arg_help(cli
, 0,
2972 "in", "Set incoming filter",
2973 "out", "Set outgoing filter", NULL
);
2977 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2978 "NAME", "Filter name", NULL
);
2981 return cli_arg_help(cli
, argc
> 1, NULL
);
2985 if (!config
->cluster_iam_master
)
2987 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2988 fmtaddr(config
->cluster_master_address
, 0));
2993 if (argc
!= 3 && argc
!= 5)
2995 cli_error(cli
, "Specify a user and filters");
2999 if (!(s
= sessionbyuser(argv
[0])))
3001 cli_error(cli
, "User %s is not connected", argv
[0]);
3005 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
3006 for (i
= 1; i
< argc
; i
+= 2)
3011 if (MATCH("in", argv
[i
]))
3013 if (session
[s
].filter_in
)
3015 cli_error(cli
, "Input already filtered");
3018 f
= &cli_session_actions
[s
].filter_in
;
3020 else if (MATCH("out", argv
[i
]))
3022 if (session
[s
].filter_out
)
3024 cli_error(cli
, "Output already filtered");
3027 f
= &cli_session_actions
[s
].filter_out
;
3031 cli_error(cli
, "Invalid filter specification");
3035 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
3036 if (v
< 0 || !*ip_filters
[v
].name
)
3038 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
3045 cli_print(cli
, "Filtering user %s", argv
[0]);
3046 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
3051 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3056 if (CLI_HELP_REQUESTED
)
3057 return cli_arg_help(cli
, argc
> 1,
3058 "USER", "Username of session to remove filters from", NULL
);
3060 if (!config
->cluster_iam_master
)
3062 cli_error(cli
, "Can't do this on a slave. Do it on %s",
3063 fmtaddr(config
->cluster_master_address
, 0));
3070 cli_error(cli
, "Specify a user to remove filters from");
3074 for (i
= 0; i
< argc
; i
++)
3076 if (!(s
= sessionbyuser(argv
[i
])))
3078 cli_error(cli
, "User %s is not connected", argv
[i
]);
3082 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3084 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3085 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3089 cli_error(cli
, "User %s not filtered", argv
[i
]);
3096 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3100 if (CLI_HELP_REQUESTED
)
3101 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3105 cli_error(cli
, "Specify a filter name");
3109 for (i
= 0; i
< argc
; i
++)
3111 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
3112 ip_filter_rulet
*rules
;
3114 if (f
< 0 || !*ip_filters
[f
].name
)
3116 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3123 cli_print(cli
, "%s IP access list %s",
3124 ip_filters
[f
].extended
? "Extended" : "Standard",
3125 ip_filters
[f
].name
);
3127 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3129 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3131 cli_print(cli
, "%s (%u match%s)", r
,
3132 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3134 cli_print(cli
, "%s", r
);
3141 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3143 if (CLI_HELP_REQUESTED
)
3144 return CLI_HELP_NO_ARGS
;
3146 kill(getppid(), SIGQUIT
);
3150 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3152 if (CLI_HELP_REQUESTED
)
3153 return CLI_HELP_NO_ARGS
;
3155 kill(getppid(), SIGHUP
);