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)))
140 struct cli_command
*c
;
141 struct cli_command
*c2
;
145 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
146 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
148 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
150 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
151 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
152 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
153 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
154 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
155 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
156 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
157 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
158 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
159 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
160 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");
161 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
162 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
164 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
165 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
166 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
169 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
171 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
172 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
175 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
176 cli_register_command(cli
, NULL
, "shutdown", cmd_shutdown
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Shutdown l2tpns daemon and exit");
177 cli_register_command(cli
, NULL
, "reload", cmd_reload
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Reload configuration");
179 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
180 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
181 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
183 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
184 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
185 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
186 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
189 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
190 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
193 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
194 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
195 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
196 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
197 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
200 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
201 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
203 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
204 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
206 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
207 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
209 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
211 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
212 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
215 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
216 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
217 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
218 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
220 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
221 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
223 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
224 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
226 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
228 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
229 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
231 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
232 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
234 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
235 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
236 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
238 // Enable regular processing
239 cli_regular(cli
, regular_stuff
);
241 if (!(f
= fopen(CLIUSERS
, "r")))
243 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
247 while (fgets(buf
, 4096, f
))
250 if (*buf
== '#') continue;
251 if ((p
= strchr(buf
, '\r'))) *p
= 0;
252 if ((p
= strchr(buf
, '\n'))) *p
= 0;
254 if (!(p
= strchr((char *)buf
, ':'))) continue;
256 if (!strcmp(buf
, "enable"))
258 cli_allow_enable(cli
, p
);
259 LOG(3, 0, 0, "Setting enable password\n");
263 cli_allow_user(cli
, buf
, p
);
264 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
271 void cli_init_complete(char *hostname
)
274 struct sockaddr_in addr
;
276 if (hostname
&& *hostname
)
277 cli_set_hostname(cli
, hostname
);
279 cli_set_hostname(cli
, "l2tpns");
281 memset(&addr
, 0, sizeof(addr
));
282 clifd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
283 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
286 // Set cli fd as non-blocking
287 flags
= fcntl(clifd
, F_GETFL
, 0);
288 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
290 addr
.sin_family
= AF_INET
;
291 addr
.sin_addr
.s_addr
= config
->cli_bind_address
; /* defaults to INADDR_ANY */
292 addr
.sin_port
= htons(23);
293 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
295 LOG(0, 0, 0, "Error binding cli on port 23: %s\n", strerror(errno
));
300 if (listen(clifd
, 10) < 0)
302 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
309 void cli_do(int sockfd
)
311 int require_auth
= 1;
312 struct sockaddr_in addr
;
313 socklen_t l
= sizeof(addr
);
315 if (fork_and_close()) return;
316 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
318 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
319 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
320 fmtaddr(addr
.sin_addr
.s_addr
, 0));
323 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
327 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
328 if (!cli
->users
) /* paranoia */
330 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
336 /* no username/pass required */
341 debug_rb_tail
= ringbuffer
->tail
;
343 memset(&debug_flags
, 0, sizeof(debug_flags
));
344 debug_flags
.critical
= 1;
346 cli_loop(cli
, sockfd
);
349 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
350 fmtaddr(addr
.sin_addr
.s_addr
, 0));
355 static void cli_print_log(struct cli_def
*cli
, char *string
)
357 LOG(3, 0, 0, "%s\n", string
);
360 void cli_do_file(FILE *fh
)
362 LOG(3, 0, 0, "Reading configuration file\n");
363 cli_print_callback(cli
, cli_print_log
);
364 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
365 cli_print_callback(cli
, NULL
);
368 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
379 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
381 int v
= va_arg(ap
, int);
382 snprintf(buf
, sizeof(buf
), entry
, v
);
388 desc
= va_arg(ap
, char *);
390 cli_error(cli
, " %-20s %s", p
, desc
);
392 cli_error(cli
, " %s", p
);
394 entry
= desc
? va_arg(ap
, char *) : 0;
399 cli_error(cli
, " <cr>");
404 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
408 if (CLI_HELP_REQUESTED
)
409 return cli_arg_help(cli
, 1,
410 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
416 // Show individual session
417 for (i
= 0; i
< argc
; i
++)
419 unsigned int s
, b_in
, b_out
;
421 if (s
<= 0 || s
>= MAXSESSION
)
423 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
426 cli_print(cli
, "\r\nSession %d:", s
);
427 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
428 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
429 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
430 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
431 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
432 switch (session
[s
].ppp
.phase
)
435 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
440 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
441 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
442 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
444 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
445 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
446 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
447 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
448 if (session
[s
].session_timeout
)
450 clockt opened
= session
[s
].opened
;
451 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
452 opened
= bundle
[session
[s
].bundle
].online_time
;
454 cli_print(cli
, "\tSess Timeout:\t%u seconds", session
[s
].session_timeout
- (opened
? abs(time_now
- opened
) : 0));
457 if (session
[s
].idle_timeout
)
458 cli_print(cli
, "\tIdle Timeout:\t%u seconds", session
[s
].idle_timeout
- (session
[s
].last_data
? abs(time_now
- session
[s
].last_data
) : 0));
460 if (session
[s
].timeout
)
462 cli_print(cli
, "\tRemaining time:\t%u",
463 (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
464 ? (unsigned) (session
[s
].timeout
- bundle
[session
[s
].bundle
].online_time
)
465 : (unsigned) (session
[s
].timeout
- (time_now
- session
[s
].opened
)));
468 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
469 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
470 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
471 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
472 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
473 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
474 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
475 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
476 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
477 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
478 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
480 cli_print(cli
, "\tIntercepted:\tno");
482 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
484 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
485 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
486 t
? "YES" : "no", t
? " (" : "",
487 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
489 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
493 b_in
= session
[s
].tbf_in
;
494 b_out
= session
[s
].tbf_out
;
496 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
497 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
498 "ByteSent", "PackSent", "PackDrop", "PackDelay");
501 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
503 (filter_list
[b_in
].next
? "*" : " "),
504 (b_in
< 100 ? "\t" : ""),
505 filter_list
[b_in
].rate
* 8,
506 filter_list
[b_in
].credit
,
507 filter_list
[b_in
].queued
,
508 filter_list
[b_in
].b_queued
,
509 filter_list
[b_in
].p_queued
,
510 filter_list
[b_in
].b_sent
,
511 filter_list
[b_in
].p_sent
,
512 filter_list
[b_in
].p_dropped
,
513 filter_list
[b_in
].p_delayed
);
516 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
518 (filter_list
[b_out
].next
? "*" : " "),
519 (b_out
< 100 ? "\t" : ""),
520 filter_list
[b_out
].rate
* 8,
521 filter_list
[b_out
].credit
,
522 filter_list
[b_out
].queued
,
523 filter_list
[b_out
].b_queued
,
524 filter_list
[b_out
].p_queued
,
525 filter_list
[b_out
].b_sent
,
526 filter_list
[b_out
].p_sent
,
527 filter_list
[b_out
].p_dropped
,
528 filter_list
[b_out
].p_delayed
);
535 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-15s %s",
552 for (i
= 1; i
< MAXSESSION
; i
++)
555 if (!session
[i
].opened
) continue;
556 if (session
[i
].bundle
&& bundle
[session
[i
].bundle
].num_of_links
> 1)
557 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- bundle
[session
[i
].bundle
].online_time
) : 0;
559 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- (time_now
-session
[i
].opened
)) : 0;
561 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %-15s %s",
564 session
[i
].user
[0] ? session
[i
].user
: "*",
565 fmtaddr(htonl(session
[i
].ip
), 0),
566 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
567 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
568 (session
[i
].walled_garden
) ? "Y" : "N",
569 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
570 abs(time_now
- (unsigned long)session
[i
].opened
),
571 (unsigned long)session
[i
].cout
,
572 (unsigned long)session
[i
].cin
,
573 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
574 (unsigned long)(rem_time
),
575 fmtaddr(htonl(tunnel
[ session
[i
].tunnel
].ip
), 1),
576 session
[i
].calling
[0] ? session
[i
].calling
: "*");
581 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
583 int i
, x
, show_all
= 0;
591 if (CLI_HELP_REQUESTED
)
594 return cli_arg_help(cli
, 1,
595 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
598 return cli_arg_help(cli
, 1,
599 "all", "Show all tunnels, including unused",
600 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
607 if (strcmp(argv
[0], "all") == 0)
613 // Show individual tunnel
614 for (i
= 0; i
< argc
; i
++)
619 if (t
<= 0 || t
>= MAXTUNNEL
)
621 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
624 cli_print(cli
, "\r\nTunnel %d:", t
);
625 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
626 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
627 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
628 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
629 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
630 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
631 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
632 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
633 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
635 for (x
= 0; x
< MAXSESSION
; x
++)
636 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
637 sprintf(s
, "%s%u ", s
, x
);
639 cli_print(cli
, "\tSessions:\t%s", s
);
645 // Show tunnel summary
646 cli_print(cli
, "%4s %20s %20s %6s %s",
653 for (i
= 1; i
< MAXTUNNEL
; i
++)
656 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
658 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
659 cli_print(cli
, "%4d %20s %20s %6s %6d",
661 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
662 fmtaddr(htonl(tunnel
[i
].ip
), 0),
663 states
[tunnel
[i
].state
],
670 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
677 if (CLI_HELP_REQUESTED
)
678 return cli_arg_help(cli
, 1,
679 "USER", "Show details for specific username",
682 for (i
= 0; i
< MAXSESSION
; i
++)
684 if (!session
[i
].opened
) continue;
685 if (!session
[i
].user
[0]) continue;
689 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
691 if (strcmp(argv
[j
], session
[i
].user
) == 0)
693 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
694 sargv
[sargc
] = sid
[sargc
];
702 cli_print(cli
, "%s", session
[i
].user
);
706 return cmd_show_session(cli
, "users", sargv
, sargc
);
712 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
714 if (CLI_HELP_REQUESTED
)
715 return CLI_HELP_NO_ARGS
;
717 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
718 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
719 GET_STAT(tun_rx_bytes
),
720 GET_STAT(tun_rx_packets
),
721 GET_STAT(tun_rx_errors
),
722 GET_STAT(tun_rx_dropped
));
723 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
724 GET_STAT(tun_tx_bytes
),
725 GET_STAT(tun_tx_packets
),
726 GET_STAT(tun_tx_errors
));
729 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
730 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
731 GET_STAT(tunnel_rx_bytes
),
732 GET_STAT(tunnel_rx_packets
),
733 GET_STAT(tunnel_rx_errors
));
734 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
735 GET_STAT(tunnel_tx_bytes
),
736 GET_STAT(tunnel_tx_packets
),
737 GET_STAT(tunnel_tx_errors
),
738 GET_STAT(tunnel_retries
));
741 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
742 cli_print(cli
, "-----------------------------------------");
743 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
744 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
745 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
746 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
747 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
748 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
749 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
750 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
751 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
752 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
753 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
754 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
755 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
756 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
757 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
758 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
759 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
760 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
764 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
765 cli_print(cli
, "-----------------------------------------");
766 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
767 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
768 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
769 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
770 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
771 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
772 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
773 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
774 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
775 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
776 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
777 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
778 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
779 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
780 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
781 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
782 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
783 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
784 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
785 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
786 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
787 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
788 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
789 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
790 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
791 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
792 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
793 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
794 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
795 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
796 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
797 #endif /* STAT_CALLS */
800 time_t l
= GET_STAT(last_reset
);
802 char *p
= strchr(t
, '\n');
806 cli_print(cli
, "Last counter reset %s", t
);
812 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
814 if (CLI_HELP_REQUESTED
)
815 return CLI_HELP_NO_ARGS
;
817 memset(_statistics
, 0, sizeof(struct Tstats
));
818 SET_STAT(last_reset
, time(NULL
));
820 cli_print(cli
, "Counters cleared");
823 #endif /* STATISTICS */
825 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
831 if (CLI_HELP_REQUESTED
)
832 return cli_arg_help(cli
, 1,
833 "tag", "Include CVS release tag",
834 "file", "Include file versions",
837 for (i
= 0; i
< argc
; i
++)
838 if (!strcmp(argv
[i
], "tag"))
840 else if (!strcmp(argv
[i
], "file"))
843 cli_print(cli
, "L2TPNS %s", VERSION
);
846 char const *p
= strchr(cvs_name
, ':');
858 e
= strpbrk(p
, " \t$");
859 cli_print(cli
, "Tag: %.*s", (int) (e
? e
- p
+ 1 : strlen(p
)), p
);
864 extern linked_list
*loaded_plugins
;
867 cli_print(cli
, "Files:");
868 cli_print(cli
, " %s", cvs_id_arp
);
870 cli_print(cli
, " %s", cvs_id_bgp
);
872 cli_print(cli
, " %s", cvs_id_cli
);
873 cli_print(cli
, " %s", cvs_id_cluster
);
874 cli_print(cli
, " %s", cvs_id_constants
);
875 cli_print(cli
, " %s", cvs_id_control
);
876 cli_print(cli
, " %s", cvs_id_icmp
);
877 cli_print(cli
, " %s", cvs_id_l2tpns
);
878 cli_print(cli
, " %s", cvs_id_ll
);
879 cli_print(cli
, " %s", cvs_id_ppp
);
880 cli_print(cli
, " %s", cvs_id_radius
);
881 cli_print(cli
, " %s", cvs_id_tbf
);
882 cli_print(cli
, " %s", cvs_id_util
);
884 ll_reset(loaded_plugins
);
885 while ((p
= ll_next(loaded_plugins
)))
887 char const **id
= dlsym(p
, "cvs_id");
889 cli_print(cli
, " %s", *id
);
896 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
899 int used
= 0, free
= 0, show_all
= 0;
901 if (!config
->cluster_iam_master
)
903 cli_print(cli
, "Can't do this on a slave. Do it on %s",
904 fmtaddr(config
->cluster_master_address
, 0));
909 if (CLI_HELP_REQUESTED
)
912 return cli_arg_help(cli
, 1, NULL
);
914 return cli_arg_help(cli
, 1,
915 "all", "Show all pool addresses, including unused",
919 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
923 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
924 for (i
= 0; i
< MAXIPPOOL
; i
++)
926 if (!ip_address_pool
[i
].address
) continue;
927 if (ip_address_pool
[i
].assigned
)
929 cli_print(cli
, "%-15s\tY %8d %s",
930 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
931 ip_address_pool
[i
].session
,
932 session
[ip_address_pool
[i
].session
].user
);
938 if (ip_address_pool
[i
].last
)
939 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
940 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
941 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
944 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
951 cli_print(cli
, "(Not displaying unused addresses)");
953 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
957 static FILE *save_config_fh
= 0;
958 static void print_save_config(struct cli_def
*cli
, char *string
)
961 fprintf(save_config_fh
, "%s\n", string
);
964 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
966 if (CLI_HELP_REQUESTED
)
967 return CLI_HELP_NO_ARGS
;
969 if ((save_config_fh
= fopen(config
->config_file
, "w")))
971 cli_print(cli
, "Writing configuration");
972 cli_print_callback(cli
, print_save_config
);
973 cmd_show_run(cli
, command
, argv
, argc
);
974 cli_print_callback(cli
, NULL
);
975 fclose(save_config_fh
);
980 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
985 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
987 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
990 char ipv6addr
[INET6_ADDRSTRLEN
];
992 if (CLI_HELP_REQUESTED
)
993 return CLI_HELP_NO_ARGS
;
995 cli_print(cli
, "# Current configuration:");
997 for (i
= 0; config_values
[i
].key
; i
++)
999 void *value
= ((void *)config
) + config_values
[i
].offset
;
1000 if (config_values
[i
].type
== STRING
)
1001 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
1002 else if (config_values
[i
].type
== IPv4
)
1003 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
1004 else if (config_values
[i
].type
== IPv6
)
1005 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
1006 else if (config_values
[i
].type
== SHORT
)
1007 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
1008 else if (config_values
[i
].type
== BOOL
)
1009 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
1010 else if (config_values
[i
].type
== INT
)
1011 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
1012 else if (config_values
[i
].type
== UNSIGNED_LONG
)
1013 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
1016 cli_print(cli
, "# Plugins");
1017 for (i
= 0; i
< MAXPLUGINS
; i
++)
1019 if (*config
->plugins
[i
])
1021 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
1026 if (config
->as_number
)
1031 cli_print(cli
, "# BGP");
1032 cli_print(cli
, "router bgp %u", config
->as_number
);
1033 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1035 if (!config
->neighbour
[i
].name
[0])
1038 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
1040 k
= config
->neighbour
[i
].keepalive
;
1041 h
= config
->neighbour
[i
].hold
;
1048 k
= BGP_KEEPALIVE_TIME
;
1054 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1056 if (config
->neighbour
[i
].update_source
.s_addr
!= INADDR_ANY
)
1057 cli_print(cli
, " neighbour %s update-source %s",
1058 config
->neighbour
[i
].name
,
1059 inet_ntoa(config
->neighbour
[i
].update_source
));
1064 cli_print(cli
, "# Filters");
1065 for (i
= 0; i
< MAXFILTER
; i
++)
1067 ip_filter_rulet
*rules
;
1068 if (!*ip_filters
[i
].name
)
1071 cli_print(cli
, "ip access-list %s %s",
1072 ip_filters
[i
].extended
? "extended" : "standard",
1073 ip_filters
[i
].name
);
1075 rules
= ip_filters
[i
].rules
;
1076 while (rules
->action
)
1077 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1080 cli_print(cli
, "# end");
1084 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1086 int i
, free
= 0, used
= 0, show_all
= 0;
1098 if (CLI_HELP_REQUESTED
)
1101 return cli_arg_help(cli
, 1, NULL
);
1103 return cli_arg_help(cli
, 1,
1104 "all", "Show all RADIUS sessions, including unused",
1108 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1112 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1115 for (i
= 1; i
< MAXRADIUS
; i
++)
1117 if (radius
[i
].state
== RADIUSNULL
)
1122 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1124 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1128 states
[radius
[i
].state
],
1134 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1139 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1143 if (CLI_HELP_REQUESTED
)
1144 return CLI_HELP_NO_ARGS
;
1146 cli_print(cli
, "Plugins currently loaded:");
1147 for (i
= 0; i
< MAXPLUGINS
; i
++)
1148 if (*config
->plugins
[i
])
1149 cli_print(cli
, " %s", config
->plugins
[i
]);
1154 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1158 if (CLI_HELP_REQUESTED
)
1159 return CLI_HELP_NO_ARGS
;
1161 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1170 for (i
= 0; i
< MAXSESSION
; i
++)
1172 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1173 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1177 session
[i
].throttle_in
,
1178 session
[i
].throttle_out
,
1180 session
[i
].tbf_out
);
1186 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1188 if (CLI_HELP_REQUESTED
)
1189 return CLI_HELP_NO_ARGS
;
1191 cli_print(cli
, " _\n"
1197 " ( \\ `. `-. _,.-:\\\n"
1198 " \\ \\ `. `-._ __..--' ,-';/\n"
1199 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1200 " `. `. `-._ __..--' ,' /\n"
1201 " `. `-_ ``--..'' _.-' ,'\n"
1202 " `-_ `-.___ __,--' ,'\n"
1203 " `-.__ `----\"\"\" __.-'\n"
1204 "hh `--..____..--'");
1209 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1214 if (CLI_HELP_REQUESTED
)
1215 return cli_arg_help(cli
, argc
> 1,
1216 "USER", "Username of session to drop", NULL
);
1218 if (!config
->cluster_iam_master
)
1220 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1221 fmtaddr(config
->cluster_master_address
, 0));
1228 cli_error(cli
, "Specify a user to drop");
1232 for (i
= 0; i
< argc
; i
++)
1234 if (!(s
= sessionbyuser(argv
[i
])))
1236 cli_error(cli
, "User %s is not connected", argv
[i
]);
1240 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1242 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1243 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1250 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1255 if (CLI_HELP_REQUESTED
)
1256 return cli_arg_help(cli
, argc
> 1,
1257 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1259 if (!config
->cluster_iam_master
)
1261 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1262 fmtaddr(config
->cluster_master_address
, 0));
1269 cli_error(cli
, "Specify a tunnel to drop");
1273 for (i
= 0; i
< argc
; i
++)
1275 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1277 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1283 cli_error(cli
, "Tunnel %d is not connected", t
);
1289 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1293 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1294 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1300 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1305 if (CLI_HELP_REQUESTED
)
1306 return cli_arg_help(cli
, argc
> 1,
1307 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1309 if (!config
->cluster_iam_master
)
1311 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1312 fmtaddr(config
->cluster_master_address
, 0));
1319 cli_error(cli
, "Specify a session id to drop");
1323 for (i
= 0; i
< argc
; i
++)
1325 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1327 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1331 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1333 cli_print(cli
, "Dropping session %d", s
);
1334 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1338 cli_error(cli
, "Session %d is not active.", s
);
1345 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1351 if (CLI_HELP_REQUESTED
)
1356 return cli_arg_help(cli
, 0,
1357 "USER", "Username of session to snoop", NULL
);
1360 return cli_arg_help(cli
, 0,
1361 "A.B.C.D", "IP address of snoop destination", NULL
);
1364 return cli_arg_help(cli
, 0,
1365 "N", "Port of snoop destination", NULL
);
1369 return cli_arg_help(cli
, 1, NULL
);
1376 if (!config
->cluster_iam_master
)
1378 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1379 fmtaddr(config
->cluster_master_address
, 0));
1386 cli_error(cli
, "Specify username, ip and port");
1390 if (!(s
= sessionbyuser(argv
[0])))
1392 cli_error(cli
, "User %s is not connected", argv
[0]);
1396 ip
= inet_addr(argv
[1]);
1397 if (!ip
|| ip
== INADDR_NONE
)
1399 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1403 port
= atoi(argv
[2]);
1406 cli_error(cli
, "Invalid port %s", argv
[2]);
1410 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1411 cli_session_actions
[s
].snoop_ip
= ip
;
1412 cli_session_actions
[s
].snoop_port
= port
;
1413 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1418 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1423 if (CLI_HELP_REQUESTED
)
1424 return cli_arg_help(cli
, argc
> 1,
1425 "USER", "Username of session to unsnoop", NULL
);
1427 if (!config
->cluster_iam_master
)
1429 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1430 fmtaddr(config
->cluster_master_address
, 0));
1437 cli_error(cli
, "Specify a user to unsnoop");
1441 for (i
= 0; i
< argc
; i
++)
1443 if (!(s
= sessionbyuser(argv
[i
])))
1445 cli_error(cli
, "User %s is not connected", argv
[i
]);
1449 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1450 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1456 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1463 throttle USER - throttle in/out to default rate
1464 throttle USER RATE - throttle in/out to default rate
1465 throttle USER in RATE - throttle input only
1466 throttle USER out RATE - throttle output only
1467 throttle USER in RATE out RATE - throttle both
1470 if (CLI_HELP_REQUESTED
)
1475 return cli_arg_help(cli
, 0,
1476 "USER", "Username of session to throttle", NULL
);
1479 return cli_arg_help(cli
, 1,
1480 "RATE", "Rate in kbps (in and out)",
1481 "in", "Select incoming rate",
1482 "out", "Select outgoing rate", NULL
);
1485 return cli_arg_help(cli
, 1,
1486 "in", "Select incoming rate",
1487 "out", "Select outgoing rate", NULL
);
1490 if (isdigit(argv
[1][0]))
1491 return cli_arg_help(cli
, 1, NULL
);
1494 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1497 return cli_arg_help(cli
, argc
> 1, NULL
);
1501 if (!config
->cluster_iam_master
)
1503 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1504 fmtaddr(config
->cluster_master_address
, 0));
1511 cli_error(cli
, "Specify a user to throttle");
1515 if (!(s
= sessionbyuser(argv
[0])))
1517 cli_error(cli
, "User %s is not connected", argv
[0]);
1523 rate_in
= rate_out
= config
->rl_rate
;
1527 rate_in
= rate_out
= atoi(argv
[1]);
1530 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1534 else if (argc
== 3 || argc
== 5)
1537 for (i
= 1; i
< argc
- 1; i
+= 2)
1540 if (MATCH("in", argv
[i
]))
1541 r
= rate_in
= atoi(argv
[i
+1]);
1542 else if (MATCH("out", argv
[i
]))
1543 r
= rate_out
= atoi(argv
[i
+1]);
1547 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1554 cli_error(cli
, "Invalid arguments");
1558 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1560 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1564 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1565 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1566 cli_session_actions
[s
].throttle_in
= rate_in
;
1568 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1569 cli_session_actions
[s
].throttle_out
= rate_out
;
1571 if (cli_session_actions
[s
].throttle_in
== -1 &&
1572 cli_session_actions
[s
].throttle_out
== -1)
1574 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1578 cli_print(cli
, "Throttling user %s", argv
[0]);
1579 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1584 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1589 if (CLI_HELP_REQUESTED
)
1590 return cli_arg_help(cli
, argc
> 1,
1591 "USER", "Username of session to unthrottle", NULL
);
1593 if (!config
->cluster_iam_master
)
1595 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1596 fmtaddr(config
->cluster_master_address
, 0));
1603 cli_error(cli
, "Specify a user to unthrottle");
1607 for (i
= 0; i
< argc
; i
++)
1609 if (!(s
= sessionbyuser(argv
[i
])))
1611 cli_error(cli
, "User %s is not connected", argv
[i
]);
1615 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1617 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1618 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1622 cli_error(cli
, "User %s not throttled", argv
[i
]);
1629 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1633 if (CLI_HELP_REQUESTED
)
1634 return cli_arg_help(cli
, 1,
1635 "all", "Enable debugging for all except \"data\"",
1636 "critical", "", // FIXME: add descriptions
1646 char *p
= (char *) &debug_flags
;
1647 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1651 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1652 (debug_flags
.critical
) ? " critical" : "",
1653 (debug_flags
.error
) ? " error" : "",
1654 (debug_flags
.warning
) ? " warning" : "",
1655 (debug_flags
.info
) ? " info" : "",
1656 (debug_flags
.calls
) ? " calls" : "",
1657 (debug_flags
.data
) ? " data" : "");
1663 cli_print(cli
, "Debugging off");
1667 for (i
= 0; i
< argc
; i
++)
1669 int len
= strlen(argv
[i
]);
1671 if (argv
[i
][0] == 'c' && len
< 2)
1672 len
= 2; /* distinguish [cr]itical from [ca]lls */
1674 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1675 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1676 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1677 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1678 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1679 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1680 if (!strncmp("all", argv
[i
], len
))
1682 memset(&debug_flags
, 1, sizeof(debug_flags
));
1683 debug_flags
.data
= 0;
1687 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1693 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1697 if (CLI_HELP_REQUESTED
)
1698 return cli_arg_help(cli
, 1,
1699 "all", "Disable all debugging",
1700 "critical", "", // FIXME: add descriptions
1710 memset(&debug_flags
, 0, sizeof(debug_flags
));
1714 for (i
= 0; i
< argc
; i
++)
1716 int len
= strlen(argv
[i
]);
1718 if (argv
[i
][0] == 'c' && len
< 2)
1719 len
= 2; /* distinguish [cr]itical from [ca]lls */
1721 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1722 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1723 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1724 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1725 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1726 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1727 if (!strncmp("all", argv
[i
], len
))
1729 memset(&debug_flags
, 0, sizeof(debug_flags
));
1733 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1739 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1741 int i
, firstfree
= 0;
1743 if (CLI_HELP_REQUESTED
)
1744 return cli_arg_help(cli
, argc
> 1,
1745 "PLUGIN", "Name of plugin to load", NULL
);
1749 cli_error(cli
, "Specify a plugin to load");
1753 for (i
= 0; i
< MAXPLUGINS
; i
++)
1755 if (!*config
->plugins
[i
] && !firstfree
)
1757 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1759 cli_error(cli
, "Plugin is already loaded");
1766 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1767 config
->reload_config
= 1;
1768 cli_print(cli
, "Loading plugin %s", argv
[0]);
1774 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1778 if (CLI_HELP_REQUESTED
)
1779 return cli_arg_help(cli
, argc
> 1,
1780 "PLUGIN", "Name of plugin to unload", NULL
);
1784 cli_error(cli
, "Specify a plugin to remove");
1788 for (i
= 0; i
< MAXPLUGINS
; i
++)
1790 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1792 config
->reload_config
= 1;
1793 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1798 cli_error(cli
, "Plugin is not loaded");
1802 static char *duration(time_t secs
)
1804 static char *buf
= NULL
;
1807 if (!buf
) buf
= calloc(64, 1);
1811 int days
= secs
/ 86400;
1812 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1818 int mins
= secs
/ 60;
1819 int hrs
= mins
/ 60;
1822 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1824 else if (secs
>= 60)
1826 int mins
= secs
/ 60;
1827 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1830 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1835 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1838 char buf
[100], *p
= buf
, *loads
[3];
1839 int i
, num_sessions
= 0;
1841 if (CLI_HELP_REQUESTED
)
1842 return CLI_HELP_NO_ARGS
;
1844 fh
= fopen("/proc/loadavg", "r");
1845 fgets(buf
, 100, fh
);
1848 for (i
= 0; i
< 3; i
++)
1849 loads
[i
] = strdup(strsep(&p
, " "));
1852 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1854 for (i
= 1; i
< MAXSESSION
; i
++)
1855 if (session
[i
].opened
) num_sessions
++;
1857 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1859 duration(time_now
- config
->start_time
),
1861 loads
[0], loads
[1], loads
[2]
1863 for (i
= 0; i
< 3; i
++)
1864 if (loads
[i
]) free(loads
[i
]);
1866 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1871 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1875 if (CLI_HELP_REQUESTED
)
1881 int len
= strlen(argv
[0])-1;
1882 for (i
= 0; config_values
[i
].key
; i
++)
1883 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1884 cli_error(cli
, " %s", config_values
[i
].key
);
1890 return cli_arg_help(cli
, 0,
1891 "VALUE", "Value for variable", NULL
);
1895 return cli_arg_help(cli
, 1, NULL
);
1904 cli_error(cli
, "Specify variable and value");
1908 for (i
= 0; config_values
[i
].key
; i
++)
1910 void *value
= ((void *) config
) + config_values
[i
].offset
;
1911 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1913 // Found a value to set
1914 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1915 switch (config_values
[i
].type
)
1918 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1921 *(int *) value
= atoi(argv
[1]);
1924 *(unsigned long *) value
= atol(argv
[1]);
1927 *(short *) value
= atoi(argv
[1]);
1930 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1933 inet_pton(AF_INET6
, argv
[1], value
);
1936 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1942 cli_error(cli
, "Unknown variable type");
1945 config
->reload_config
= 1;
1950 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1954 int regular_stuff(struct cli_def
*cli
)
1960 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1962 char *m
= ringbuffer
->buffer
[i
].message
;
1968 switch (ringbuffer
->buffer
[i
].level
)
1970 case 0: show
= debug_flags
.critical
; break;
1971 case 1: show
= debug_flags
.error
; break;
1972 case 2: show
= debug_flags
.warning
; break;
1973 case 3: show
= debug_flags
.info
; break;
1974 case 4: show
= debug_flags
.calls
; break;
1975 case 5: show
= debug_flags
.data
; break;
1978 if (!show
) continue;
1980 if (!(p
= strchr(m
, '\n')))
1983 cli_error(cli
, "\r%s-%u-%u %.*s",
1984 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1985 ringbuffer
->buffer
[i
].tunnel
,
1986 ringbuffer
->buffer
[i
].session
,
1992 debug_rb_tail
= ringbuffer
->tail
;
2000 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2004 if (CLI_HELP_REQUESTED
)
2005 return cli_arg_help(cli
, argc
> 1,
2006 "<1-65535>", "Autonomous system number", NULL
);
2008 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
2010 cli_error(cli
, "Invalid autonomous system number");
2014 if (bgp_configured
&& as
!= config
->as_number
)
2016 cli_error(cli
, "Can't change local AS on a running system");
2020 config
->as_number
= as
;
2021 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
2026 static int find_bgp_neighbour(char const *name
)
2031 in_addr_t addrs
[4] = { 0 };
2034 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
2037 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
2038 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
2040 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2042 if (!config
->neighbour
[i
].name
[0])
2044 if (new == -1) new = i
;
2048 if (!strcmp(name
, config
->neighbour
[i
].name
))
2051 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
2054 for (a
= h
->h_addr_list
; *a
; a
++)
2057 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
2058 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2066 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2072 if (CLI_HELP_REQUESTED
)
2077 return cli_arg_help(cli
, 0,
2078 "A.B.C.D", "BGP neighbour address",
2079 "NAME", "BGP neighbour name",
2083 return cli_arg_help(cli
, 0,
2084 "remote-as", "Set remote autonomous system number",
2085 "timers", "Set timers",
2086 "update-source", "Set source address to use for the BGP session",
2090 if (MATCH("remote-as", argv
[1]))
2091 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2093 if (MATCH("timers", argv
[1]))
2096 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2099 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2101 if (argc
== 5 && !argv
[4][1])
2102 return cli_arg_help(cli
, 1, NULL
);
2105 if (MATCH("update-source", argv
[1]))
2106 return cli_arg_help(cli
, argc
> 3, "A.B.C.D", "Source IP address", NULL
);
2114 cli_error(cli
, "Invalid arguments");
2118 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2120 cli_error(cli
, "Invalid neighbour");
2126 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2130 if (MATCH("remote-as", argv
[1]))
2132 int as
= atoi(argv
[2]);
2133 if (as
< 0 || as
> 65535)
2135 cli_error(cli
, "Invalid autonomous system number");
2139 if (!config
->neighbour
[i
].name
[0])
2141 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2142 config
->neighbour
[i
].keepalive
= -1;
2143 config
->neighbour
[i
].hold
= -1;
2144 config
->neighbour
[i
].update_source
.s_addr
= INADDR_ANY
;
2147 config
->neighbour
[i
].as
= as
;
2151 if (MATCH("update-source", argv
[1]))
2153 struct in_addr addr
;
2155 if (!config
->neighbour
[i
].name
[0])
2157 cli_error(cli
, "Specify remote-as first");
2161 if (!inet_aton(argv
[2], &addr
))
2163 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[2]);
2167 config
->neighbour
[i
].update_source
= addr
;
2171 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2173 cli_error(cli
, "Invalid arguments");
2177 if (!config
->neighbour
[i
].name
[0])
2179 cli_error(cli
, "Specify remote-as first");
2183 keepalive
= atoi(argv
[2]);
2184 hold
= atoi(argv
[3]);
2186 if (keepalive
< 1 || keepalive
> 65535)
2188 cli_error(cli
, "Invalid keepalive time");
2192 if (hold
< 3 || hold
> 65535)
2194 cli_error(cli
, "Invalid hold time");
2198 if (keepalive
== BGP_KEEPALIVE_TIME
)
2199 keepalive
= -1; // using default value
2201 if (hold
== BGP_HOLD_TIME
)
2204 config
->neighbour
[i
].keepalive
= keepalive
;
2205 config
->neighbour
[i
].hold
= hold
;
2210 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2214 if (CLI_HELP_REQUESTED
)
2215 return cli_arg_help(cli
, argc
> 0,
2216 "A.B.C.D", "BGP neighbour address",
2217 "NAME", "BGP neighbour name",
2222 cli_error(cli
, "Specify a BGP neighbour");
2226 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2228 cli_error(cli
, "Invalid neighbour");
2232 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2234 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2238 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2242 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2248 if (!bgp_configured
)
2251 if (CLI_HELP_REQUESTED
)
2252 return cli_arg_help(cli
, 1,
2253 "A.B.C.D", "BGP neighbour address",
2254 "NAME", "BGP neighbour name",
2257 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2258 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2262 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2264 if (!*bgp_peers
[i
].name
)
2267 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2268 if (argc
&& strcmp(addr
, argv
[0]) &&
2269 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2275 cli_print(cli
, "Peer AS Address "
2276 "State Retries Retry in Route Pend Timers");
2277 cli_print(cli
, "------------------ ----- --------------- "
2278 "----------- ------- -------- ----- ---- ---------");
2281 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2285 bgp_state_str(bgp_peers
[i
].state
),
2286 bgp_peers
[i
].retry_count
,
2287 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2288 bgp_peers
[i
].routing
? "yes" : "no",
2289 bgp_peers
[i
].update_routes
? "yes" : "no",
2290 bgp_peers
[i
].keepalive
,
2297 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2302 if (!bgp_configured
)
2305 if (CLI_HELP_REQUESTED
)
2306 return cli_arg_help(cli
, 1,
2307 "A.B.C.D", "BGP neighbour address",
2308 "NAME", "BGP neighbour name",
2311 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2313 if (bgp_peers
[i
].state
!= Established
)
2316 if (!bgp_peers
[i
].routing
)
2319 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2320 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2323 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2324 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2330 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2335 if (!bgp_configured
)
2338 if (CLI_HELP_REQUESTED
)
2339 return cli_arg_help(cli
, 1,
2340 "A.B.C.D", "BGP neighbour address",
2341 "NAME", "BGP neighbour name",
2344 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2346 if (bgp_peers
[i
].state
!= Established
)
2349 if (bgp_peers
[i
].routing
)
2352 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2353 if (argc
&& strcmp(addr
, argv
[0]) &&
2354 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2357 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2358 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2364 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2369 if (!bgp_configured
)
2372 if (CLI_HELP_REQUESTED
)
2373 return cli_arg_help(cli
, 1,
2374 "A.B.C.D", "BGP neighbour address",
2375 "NAME", "BGP neighbour name",
2378 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2380 if (!*bgp_peers
[i
].name
)
2383 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2384 if (argc
&& strcmp(addr
, argv
[0]) &&
2385 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2388 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2389 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2397 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2401 if (CLI_HELP_REQUESTED
)
2406 return cli_arg_help(cli
, 0,
2407 "standard", "Standard syntax",
2408 "extended", "Extended syntax",
2412 return cli_arg_help(cli
, argv
[1][1],
2413 "NAME", "Access-list name",
2417 if (argc
== 3 && !argv
[2][1])
2418 return cli_arg_help(cli
, 1, NULL
);
2426 cli_error(cli
, "Specify access-list type and name");
2430 if (MATCH("standard", argv
[0]))
2432 else if (MATCH("extended", argv
[0]))
2436 cli_error(cli
, "Invalid access-list type");
2440 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2441 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2443 cli_error(cli
, "Invalid access-list name");
2447 filt
= find_filter(argv
[1], strlen(argv
[1]));
2452 cli_error(cli
, "Too many access-lists");
2457 if (!*ip_filters
[filt
].name
)
2459 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2460 strcpy(ip_filters
[filt
].name
, argv
[1]);
2461 ip_filters
[filt
].extended
= extended
;
2463 else if (ip_filters
[filt
].extended
!= extended
)
2465 cli_error(cli
, "Access-list is %s",
2466 ip_filters
[filt
].extended
? "extended" : "standard");
2471 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2475 if (filt
< 0 || !*ip_filters
[filt
].name
)
2477 cli_error(cli
, "Access-list not defined");
2482 if (ip_filters
[filt
].used
)
2484 cli_error(cli
, "Access-list in use");
2488 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2492 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2494 return access_list(cli
, argv
, argc
, 1);
2497 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2499 return access_list(cli
, argv
, argc
, 0);
2502 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2504 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2505 return sprintf(buf
, " any");
2507 if (wild
== INADDR_ANY
)
2508 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2510 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2513 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2517 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2518 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2519 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2520 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2521 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2527 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2529 static char buf
[256];
2532 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2535 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2536 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2539 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2543 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2544 p
+= show_ports(p
, &rule
->src_ports
);
2546 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2547 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2548 p
+= show_ports(p
, &rule
->dst_ports
);
2550 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2552 switch (rule
->tcp_flag_op
)
2554 case FILTER_FLAG_OP_EST
:
2555 p
+= sprintf(p
, " established");
2558 case FILTER_FLAG_OP_ANY
:
2559 case FILTER_FLAG_OP_ALL
:
2560 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2561 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2562 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2563 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2564 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2565 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2566 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2567 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2568 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2569 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2570 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2571 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2572 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2578 p
+= sprintf(p
, " fragments");
2583 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2585 static ip_filter_rulet rule
;
2586 struct in_addr addr
;
2590 if (CLI_HELP_REQUESTED
)
2594 cli_arg_help(cli
, 0,
2595 "ip", "Match IP packets",
2596 "tcp", "Match TCP packets",
2597 "udp", "Match UDP packets",
2603 // *sigh*, too darned complex
2604 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2610 cli_error(cli
, "Specify rule details");
2614 memset(&rule
, 0, sizeof(rule
));
2615 rule
.action
= (command
[0] == 'p')
2616 ? FILTER_ACTION_PERMIT
2617 : FILTER_ACTION_DENY
;
2619 if (MATCH("ip", argv
[0]))
2620 rule
.proto
= IPPROTO_IP
;
2621 else if (MATCH("udp", argv
[0]))
2622 rule
.proto
= IPPROTO_UDP
;
2623 else if (MATCH("tcp", argv
[0]))
2624 rule
.proto
= IPPROTO_TCP
;
2627 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2631 for (a
= 1, i
= 0; i
< 2; i
++)
2635 ip_filter_portt
*port
;
2640 wild
= &rule
.src_wild
;
2641 port
= &rule
.src_ports
;
2646 wild
= &rule
.dst_wild
;
2647 port
= &rule
.dst_ports
;
2650 cli_error(cli
, "Specify destination");
2655 if (MATCH("any", argv
[a
]))
2658 *wild
= INADDR_BROADCAST
;
2661 else if (MATCH("host", argv
[a
]))
2665 cli_error(cli
, "Specify host ip address");
2669 if (!inet_aton(argv
[a
], &addr
))
2671 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2683 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2687 if (!inet_aton(argv
[a
], &addr
))
2689 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2695 if (!inet_aton(argv
[++a
], &addr
))
2697 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2701 *wild
= addr
.s_addr
;
2705 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2709 if (MATCH("eq", argv
[a
]))
2710 port
->op
= FILTER_PORT_OP_EQ
;
2711 else if (MATCH("neq", argv
[a
]))
2712 port
->op
= FILTER_PORT_OP_NEQ
;
2713 else if (MATCH("gt", argv
[a
]))
2714 port
->op
= FILTER_PORT_OP_GT
;
2715 else if (MATCH("lt", argv
[a
]))
2716 port
->op
= FILTER_PORT_OP_LT
;
2717 else if (MATCH("range", argv
[a
]))
2718 port
->op
= FILTER_PORT_OP_RANGE
;
2725 cli_error(cli
, "Specify port");
2729 if (!(port
->port
= atoi(argv
[a
])))
2731 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2736 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2741 cli_error(cli
, "Specify port");
2745 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2747 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2754 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2756 if (MATCH("established", argv
[a
]))
2758 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2761 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2762 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2764 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2765 ? FILTER_FLAG_OP_ANY
2766 : FILTER_FLAG_OP_ALL
;
2770 cli_error(cli
, "Specify tcp flags");
2774 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2778 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2780 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2781 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2782 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2783 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2784 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2785 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2788 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2797 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2799 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2801 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2811 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2818 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2820 static ip_filter_rulet rule
;
2821 struct in_addr addr
;
2823 if (CLI_HELP_REQUESTED
)
2827 cli_arg_help(cli
, argv
[0][1],
2828 "A.B.C.D", "Source address",
2829 "any", "Any source address",
2830 "host", "Source host",
2836 if (MATCH("any", argv
[0]))
2838 if (argc
== 2 && !argv
[1][1])
2839 cli_arg_help(cli
, 1, NULL
);
2841 else if (MATCH("host", argv
[0]))
2845 cli_arg_help(cli
, argv
[1][1],
2846 "A.B.C.D", "Host address",
2849 else if (argc
== 3 && !argv
[2][1])
2850 cli_arg_help(cli
, 1, NULL
);
2856 cli_arg_help(cli
, 1,
2857 "A.B.C.D", "Wildcard bits",
2860 else if (argc
== 3 && !argv
[2][1])
2861 cli_arg_help(cli
, 1, NULL
);
2869 cli_error(cli
, "Specify rule details");
2873 memset(&rule
, 0, sizeof(rule
));
2874 rule
.action
= (command
[0] == 'p')
2875 ? FILTER_ACTION_PERMIT
2876 : FILTER_ACTION_DENY
;
2878 rule
.proto
= IPPROTO_IP
;
2879 if (MATCH("any", argv
[0]))
2881 rule
.src_ip
= INADDR_ANY
;
2882 rule
.src_wild
= INADDR_BROADCAST
;
2884 else if (MATCH("host", argv
[0]))
2888 cli_error(cli
, "Specify host ip address");
2892 if (!inet_aton(argv
[1], &addr
))
2894 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2898 rule
.src_ip
= addr
.s_addr
;
2899 rule
.src_wild
= INADDR_ANY
;
2905 cli_error(cli
, "Specify source ip address and wildcard");
2909 if (!inet_aton(argv
[0], &addr
))
2911 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2915 rule
.src_ip
= addr
.s_addr
;
2919 if (!inet_aton(argv
[1], &addr
))
2921 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2925 rule
.src_wild
= addr
.s_addr
;
2928 rule
.src_wild
= INADDR_ANY
;
2934 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2937 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2938 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2939 : access_list_rule_std(cli
, command
, argv
, argc
);
2944 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2946 if (!ip_filters
[filt
].rules
[i
].action
)
2948 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2952 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2956 cli_error(cli
, "Too many rules");
2960 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2965 /* filter USER {in|out} FILTER ... */
2966 if (CLI_HELP_REQUESTED
)
2971 return cli_arg_help(cli
, 0,
2972 "USER", "Username of session to filter", NULL
);
2976 return cli_arg_help(cli
, 0,
2977 "in", "Set incoming filter",
2978 "out", "Set outgoing filter", NULL
);
2982 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2983 "NAME", "Filter name", NULL
);
2986 return cli_arg_help(cli
, argc
> 1, NULL
);
2990 if (!config
->cluster_iam_master
)
2992 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2993 fmtaddr(config
->cluster_master_address
, 0));
2998 if (argc
!= 3 && argc
!= 5)
3000 cli_error(cli
, "Specify a user and filters");
3004 if (!(s
= sessionbyuser(argv
[0])))
3006 cli_error(cli
, "User %s is not connected", argv
[0]);
3010 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
3011 for (i
= 1; i
< argc
; i
+= 2)
3016 if (MATCH("in", argv
[i
]))
3018 if (session
[s
].filter_in
)
3020 cli_error(cli
, "Input already filtered");
3023 f
= &cli_session_actions
[s
].filter_in
;
3025 else if (MATCH("out", argv
[i
]))
3027 if (session
[s
].filter_out
)
3029 cli_error(cli
, "Output already filtered");
3032 f
= &cli_session_actions
[s
].filter_out
;
3036 cli_error(cli
, "Invalid filter specification");
3040 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
3041 if (v
< 0 || !*ip_filters
[v
].name
)
3043 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
3050 cli_print(cli
, "Filtering user %s", argv
[0]);
3051 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
3056 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3061 if (CLI_HELP_REQUESTED
)
3062 return cli_arg_help(cli
, argc
> 1,
3063 "USER", "Username of session to remove filters from", NULL
);
3065 if (!config
->cluster_iam_master
)
3067 cli_error(cli
, "Can't do this on a slave. Do it on %s",
3068 fmtaddr(config
->cluster_master_address
, 0));
3075 cli_error(cli
, "Specify a user to remove filters from");
3079 for (i
= 0; i
< argc
; i
++)
3081 if (!(s
= sessionbyuser(argv
[i
])))
3083 cli_error(cli
, "User %s is not connected", argv
[i
]);
3087 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3089 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3090 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3094 cli_error(cli
, "User %s not filtered", argv
[i
]);
3101 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3105 if (CLI_HELP_REQUESTED
)
3106 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3110 cli_error(cli
, "Specify a filter name");
3114 for (i
= 0; i
< argc
; i
++)
3116 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
3117 ip_filter_rulet
*rules
;
3119 if (f
< 0 || !*ip_filters
[f
].name
)
3121 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3128 cli_print(cli
, "%s IP access list %s",
3129 ip_filters
[f
].extended
? "Extended" : "Standard",
3130 ip_filters
[f
].name
);
3132 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3134 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3136 cli_print(cli
, "%s (%u match%s)", r
,
3137 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3139 cli_print(cli
, "%s", r
);
3146 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3148 if (CLI_HELP_REQUESTED
)
3149 return CLI_HELP_NO_ARGS
;
3151 kill(getppid(), SIGQUIT
);
3155 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3157 if (CLI_HELP_REQUESTED
)
3158 return CLI_HELP_NO_ARGS
;
3160 kill(getppid(), SIGHUP
);