1 // L2TPNS Command Line Interface
16 #include <arpa/inet.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
24 #include <linux/rtnetlink.h>
28 #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
, const char *command
, char **argv
, int argc
);
80 static int cmd_show_group(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
81 static int cmd_show_tunnels(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
82 static int cmd_show_users(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
83 static int cmd_show_radius(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
84 static int cmd_show_version(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
85 static int cmd_show_pool(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
86 static int cmd_show_run(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
87 static int cmd_show_banana(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
88 static int cmd_show_plugins(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
89 static int cmd_show_throttle(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
90 static int cmd_write_memory(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
91 static int cmd_drop_user(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
92 static int cmd_drop_tunnel(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
93 static int cmd_drop_session(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
94 static int cmd_snoop(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
95 static int cmd_no_snoop(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
96 static int cmd_throttle(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
97 static int cmd_no_throttle(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
98 static int cmd_debug(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
99 static int cmd_no_debug(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
100 static int cmd_set(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
101 static int cmd_load_plugin(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
102 static int cmd_remove_plugin(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
103 static int cmd_uptime(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
104 static int cmd_shutdown(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
105 static int cmd_reload(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
106 static int cmd_setforward(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
107 static int cmd_show_rmtlnsconf(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
109 static int regular_stuff(struct cli_def
*cli
);
112 static int cmd_show_counters(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
113 static int cmd_clear_counters(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
114 #endif /* STATISTICS */
117 #define MODE_CONFIG_BGP 8
118 static int cmd_router_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
119 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
120 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
121 static int cmd_show_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
122 static int cmd_suspend_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
123 static int cmd_no_suspend_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
124 static int cmd_restart_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
127 #define MODE_CONFIG_NACL 9
128 static int cmd_ip_access_list(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
129 static int cmd_no_ip_access_list(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
130 static int cmd_ip_access_list_rule(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
131 static int cmd_filter(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
132 static int cmd_no_filter(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
133 static int cmd_show_access_list(struct cli_def
*cli
, const char *command
, char **argv
, int argc
);
135 /* match if b is a substr of a */
136 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
142 struct cli_command
*c
;
143 struct cli_command
*c2
;
147 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
148 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
150 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
152 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
153 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
154 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
155 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
156 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
157 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
158 cli_register_command(cli
, c
, "remotelns-conf", cmd_show_rmtlnsconf
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show a list of remote LNS configuration");
159 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
160 cli_register_command(cli
, c
, "group", cmd_show_group
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of groups or details for a single group");
161 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
162 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
163 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
164 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");
165 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
166 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
168 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
169 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
170 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
173 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
175 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
176 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
179 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
180 cli_register_command(cli
, NULL
, "shutdown", cmd_shutdown
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Shutdown l2tpns daemon and exit");
181 cli_register_command(cli
, NULL
, "reload", cmd_reload
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Reload configuration");
183 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
184 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
185 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
187 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
188 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
189 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
190 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
193 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
194 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
197 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
198 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
199 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
200 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
201 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
204 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
205 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
207 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
208 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
210 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
211 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
213 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
215 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
216 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
219 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
220 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
221 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
222 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
224 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
225 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
227 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
228 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
230 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
232 cli_register_command(cli
, NULL
, "setforward", cmd_setforward
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set the Remote LNS Forward");
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
);
277 void cli_init_complete(char *hostname
)
280 struct sockaddr_in addr
;
282 if (hostname
&& *hostname
)
283 cli_set_hostname(cli
, hostname
);
285 cli_set_hostname(cli
, "l2tpns");
287 memset(&addr
, 0, sizeof(addr
));
288 clifd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
289 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
292 // Set cli fd as non-blocking
293 flags
= fcntl(clifd
, F_GETFL
, 0);
294 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
296 addr
.sin_family
= AF_INET
;
297 addr
.sin_addr
.s_addr
= config
->cli_bind_address
; /* defaults to INADDR_ANY */
298 addr
.sin_port
= htons(23);
299 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
301 LOG(0, 0, 0, "Error binding cli on port 23: %s\n", strerror(errno
));
306 if (listen(clifd
, 10) < 0)
308 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
315 void cli_do(int sockfd
)
317 int require_auth
= 1;
318 struct sockaddr_in addr
;
319 socklen_t l
= sizeof(addr
);
321 if (fork_and_close()) return;
322 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
324 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
325 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
326 fmtaddr(addr
.sin_addr
.s_addr
, 0));
329 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
333 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
334 if (!cli
->users
) /* paranoia */
336 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
342 /* no username/pass required */
347 debug_rb_tail
= ringbuffer
->tail
;
349 memset(&debug_flags
, 0, sizeof(debug_flags
));
350 debug_flags
.critical
= 1;
352 cli_loop(cli
, sockfd
);
355 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
356 fmtaddr(addr
.sin_addr
.s_addr
, 0));
361 static void cli_print_log(struct cli_def
*cli
, const char *string
)
363 LOG(3, 0, 0, "%s\n", string
);
366 void cli_do_file(FILE *fh
)
368 LOG(3, 0, 0, "Reading configuration file\n");
369 cli_print_callback(cli
, cli_print_log
);
370 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
371 cli_print_callback(cli
, NULL
);
374 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
385 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
387 int v
= va_arg(ap
, int);
388 snprintf(buf
, sizeof(buf
), entry
, v
);
394 desc
= va_arg(ap
, char *);
396 cli_error(cli
, " %-20s %s", p
, desc
);
398 cli_error(cli
, " %s", p
);
400 entry
= desc
? va_arg(ap
, char *) : 0;
405 cli_error(cli
, " <cr>");
410 static int cmd_show_session(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
414 if (CLI_HELP_REQUESTED
)
415 return cli_arg_help(cli
, 1,
416 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
422 // Show individual session
423 for (i
= 0; i
< argc
; i
++)
425 unsigned int s
, b_in
, b_out
;
427 if (s
<= 0 || s
>= MAXSESSION
)
429 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
432 cli_print(cli
, "\r\nSession %d:", s
);
433 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
434 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
435 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
436 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
437 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
438 switch (session
[s
].ppp
.phase
)
441 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
446 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
447 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
448 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
450 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
451 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
452 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
453 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
454 if (session
[s
].session_timeout
)
456 clockt opened
= session
[s
].opened
;
457 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
458 opened
= bundle
[session
[s
].bundle
].online_time
;
460 cli_print(cli
, "\tSess Timeout:\t%u seconds", session
[s
].session_timeout
- (opened
? abs(time_now
- opened
) : 0));
463 if (session
[s
].idle_timeout
)
464 cli_print(cli
, "\tIdle Timeout:\t%u seconds", session
[s
].idle_timeout
- (session
[s
].last_data
? abs(time_now
- session
[s
].last_data
) : 0));
466 if (session
[s
].timeout
)
468 cli_print(cli
, "\tRemaining time:\t%u",
469 (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
470 ? (unsigned) (session
[s
].timeout
- bundle
[session
[s
].bundle
].online_time
)
471 : (unsigned) (session
[s
].timeout
- (time_now
- session
[s
].opened
)));
474 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
475 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
476 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
477 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
478 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
479 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
480 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
481 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
482 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
483 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
484 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
486 cli_print(cli
, "\tIntercepted:\tno");
488 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
490 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
491 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
492 t
? "YES" : "no", t
? " (" : "",
493 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
495 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
499 b_in
= session
[s
].tbf_in
;
500 b_out
= session
[s
].tbf_out
;
502 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
503 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
504 "ByteSent", "PackSent", "PackDrop", "PackDelay");
507 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
509 (filter_list
[b_in
].next
? "*" : " "),
510 (b_in
< 100 ? "\t" : ""),
511 filter_list
[b_in
].rate
* 8,
512 filter_list
[b_in
].credit
,
513 filter_list
[b_in
].queued
,
514 filter_list
[b_in
].b_queued
,
515 filter_list
[b_in
].p_queued
,
516 filter_list
[b_in
].b_sent
,
517 filter_list
[b_in
].p_sent
,
518 filter_list
[b_in
].p_dropped
,
519 filter_list
[b_in
].p_delayed
);
522 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
524 (filter_list
[b_out
].next
? "*" : " "),
525 (b_out
< 100 ? "\t" : ""),
526 filter_list
[b_out
].rate
* 8,
527 filter_list
[b_out
].credit
,
528 filter_list
[b_out
].queued
,
529 filter_list
[b_out
].b_queued
,
530 filter_list
[b_out
].p_queued
,
531 filter_list
[b_out
].b_sent
,
532 filter_list
[b_out
].p_sent
,
533 filter_list
[b_out
].p_dropped
,
534 filter_list
[b_out
].p_delayed
);
541 cli_print(cli
, "%5s %7s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-18s %s",
556 "LAC(L)/RLNS(R)/PPPOE(P)",
559 for (i
= 1; i
< MAXSESSION
; i
++)
562 if (!session
[i
].opened
) continue;
563 if (session
[i
].bundle
&& bundle
[session
[i
].bundle
].num_of_links
> 1)
564 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- bundle
[session
[i
].bundle
].online_time
) : 0;
566 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- (time_now
-session
[i
].opened
)) : 0;
567 cli_print(cli
, "%5d %7d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-20s %s",
569 session
[i
].forwardtosession
,
571 session
[i
].user
[0] ? session
[i
].user
: "*",
572 fmtaddr(htonl(session
[i
].ip
), 0),
573 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
574 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
575 (session
[i
].walled_garden
) ? "Y" : "N",
576 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
577 abs(time_now
- (unsigned long)session
[i
].opened
),
578 (unsigned long)session
[i
].cout
,
579 (unsigned long)session
[i
].cin
,
580 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
581 (unsigned long)(rem_time
),
582 (session
[i
].tunnel
== TUNNEL_ID_PPPOE
)?"(P)":(tunnel
[session
[i
].tunnel
].isremotelns
?"(R)":"(L)"),
583 (session
[i
].tunnel
== TUNNEL_ID_PPPOE
)?fmtMacAddr(session
[i
].src_hwaddr
):fmtaddr(htonl(tunnel
[session
[i
].tunnel
].ip
), 1),
584 session
[i
].calling
[0] ? session
[i
].calling
: "*");
589 static int cmd_show_tunnels(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
591 int i
, x
, show_all
= 0;
599 if (CLI_HELP_REQUESTED
)
602 return cli_arg_help(cli
, 1,
603 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
606 return cli_arg_help(cli
, 1,
607 "all", "Show all tunnels, including unused",
608 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
615 if (strcmp(argv
[0], "all") == 0)
621 // Show individual tunnel
622 for (i
= 0; i
< argc
; i
++)
627 if (t
<= 0 || t
>= MAXTUNNEL
)
629 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
632 cli_print(cli
, "\r\nTunnel %d:", t
);
633 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
634 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
635 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
636 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
637 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
638 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
639 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
640 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
641 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
643 for (x
= 0; x
< MAXSESSION
; x
++)
644 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
645 sprintf(s
, "%s%u ", s
, x
);
647 cli_print(cli
, "\tSessions:\t%s", s
);
653 // Show tunnel summary
654 cli_print(cli
, "%4s %20s %20s %6s %s",
661 for (i
= 1; i
< MAXTUNNEL
; i
++)
664 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
666 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
667 cli_print(cli
, "%4d %20s %20s %6s %6d %s",
669 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
670 fmtaddr(htonl(tunnel
[i
].ip
), 0),
671 states
[tunnel
[i
].state
],
673 ,(i
== TUNNEL_ID_PPPOE
)?"Tunnel pppoe":(tunnel
[i
].isremotelns
?"Tunnel To Remote LNS":"Tunnel To LAC")
680 static int cmd_show_users(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
687 if (CLI_HELP_REQUESTED
)
688 return cli_arg_help(cli
, 1,
689 "USER", "Show details for specific username",
692 for (i
= 0; i
< MAXSESSION
; i
++)
694 if (!session
[i
].opened
) continue;
695 if (!session
[i
].user
[0]) continue;
699 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
701 if (strcmp(argv
[j
], session
[i
].user
) == 0)
703 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
704 sargv
[sargc
] = sid
[sargc
];
712 cli_print(cli
, "%s", session
[i
].user
);
716 return cmd_show_session(cli
, "users", sargv
, sargc
);
722 static int cmd_show_counters(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
724 if (CLI_HELP_REQUESTED
)
725 return CLI_HELP_NO_ARGS
;
727 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
728 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
729 GET_STAT(tun_rx_bytes
),
730 GET_STAT(tun_rx_packets
),
731 GET_STAT(tun_rx_errors
),
732 GET_STAT(tun_rx_dropped
));
733 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
734 GET_STAT(tun_tx_bytes
),
735 GET_STAT(tun_tx_packets
),
736 GET_STAT(tun_tx_errors
));
739 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
740 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
741 GET_STAT(tunnel_rx_bytes
),
742 GET_STAT(tunnel_rx_packets
),
743 GET_STAT(tunnel_rx_errors
));
744 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
745 GET_STAT(tunnel_tx_bytes
),
746 GET_STAT(tunnel_tx_packets
),
747 GET_STAT(tunnel_tx_errors
),
748 GET_STAT(tunnel_retries
));
751 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
752 cli_print(cli
, "-----------------------------------------");
753 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
754 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
755 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
756 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
757 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
758 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
759 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
760 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
761 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
762 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
763 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
764 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
765 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
766 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
767 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
768 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
769 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
770 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
774 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
775 cli_print(cli
, "-----------------------------------------");
776 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
777 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
778 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
779 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
780 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
781 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
782 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
783 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
784 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
785 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
786 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
787 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
788 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
789 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
790 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
791 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
792 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
793 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
794 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
795 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
796 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
797 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
798 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
799 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
800 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
801 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
802 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
803 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
804 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
805 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
806 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
807 #endif /* STAT_CALLS */
810 time_t l
= GET_STAT(last_reset
);
812 char *p
= strchr(t
, '\n');
816 cli_print(cli
, "Last counter reset %s", t
);
822 static int cmd_clear_counters(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
824 if (CLI_HELP_REQUESTED
)
825 return CLI_HELP_NO_ARGS
;
827 memset(_statistics
, 0, sizeof(struct Tstats
));
828 SET_STAT(last_reset
, time(NULL
));
830 cli_print(cli
, "Counters cleared");
833 #endif /* STATISTICS */
835 static int cmd_show_version(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
837 if (CLI_HELP_REQUESTED
)
838 return CLI_HELP_NO_ARGS
;
840 cli_print(cli
, "L2TPNS %s", VERSION
);
844 static int cmd_show_pool(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
847 int used
= 0, free
= 0, show_all
= 0;
849 if (!config
->cluster_iam_master
)
851 cli_print(cli
, "Can't do this on a slave. Do it on %s",
852 fmtaddr(config
->cluster_master_address
, 0));
857 if (CLI_HELP_REQUESTED
)
860 return cli_arg_help(cli
, 1, NULL
);
862 return cli_arg_help(cli
, 1,
863 "all", "Show all pool addresses, including unused",
867 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
871 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
872 for (i
= 0; i
< MAXIPPOOL
; i
++)
874 if (!ip_address_pool
[i
].address
) continue;
875 if (ip_address_pool
[i
].assigned
)
877 cli_print(cli
, "%-15s\tY %8d %s",
878 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
879 ip_address_pool
[i
].session
,
880 session
[ip_address_pool
[i
].session
].user
);
886 if (ip_address_pool
[i
].last
)
887 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
888 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
889 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
892 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
899 cli_print(cli
, "(Not displaying unused addresses)");
901 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
905 static FILE *save_config_fh
= 0;
906 static void print_save_config(struct cli_def
*cli
, const char *string
)
909 fprintf(save_config_fh
, "%s\n", string
);
912 static int cmd_write_memory(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
914 if (CLI_HELP_REQUESTED
)
915 return CLI_HELP_NO_ARGS
;
917 if ((save_config_fh
= fopen(config
->config_file
, "w")))
919 cli_print(cli
, "Writing configuration");
920 cli_print_callback(cli
, print_save_config
);
921 cmd_show_run(cli
, command
, argv
, argc
);
922 cli_print_callback(cli
, NULL
);
923 fclose(save_config_fh
);
928 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
933 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
935 static int cmd_show_run(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
938 char ipv6addr
[INET6_ADDRSTRLEN
];
940 if (CLI_HELP_REQUESTED
)
941 return CLI_HELP_NO_ARGS
;
943 cli_print(cli
, "# Current configuration:");
945 for (i
= 0; config_values
[i
].key
; i
++)
947 void *value
= ((void *)config
) + config_values
[i
].offset
;
948 if (config_values
[i
].type
== STRING
)
949 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
950 else if (config_values
[i
].type
== IPv4
)
951 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
952 else if (config_values
[i
].type
== IPv6
)
953 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
954 else if (config_values
[i
].type
== SHORT
)
955 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
956 else if (config_values
[i
].type
== BOOL
)
957 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
958 else if (config_values
[i
].type
== INT
)
959 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
960 else if (config_values
[i
].type
== UNSIGNED_LONG
)
961 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
964 cli_print(cli
, "# Plugins");
965 for (i
= 0; i
< MAXPLUGINS
; i
++)
967 if (*config
->plugins
[i
])
969 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
974 if (config
->as_number
)
979 cli_print(cli
, "# BGP");
980 cli_print(cli
, "router bgp %u", config
->as_number
);
981 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
983 if (!config
->neighbour
[i
].name
[0])
986 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
988 k
= config
->neighbour
[i
].keepalive
;
989 h
= config
->neighbour
[i
].hold
;
996 k
= BGP_KEEPALIVE_TIME
;
1002 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1004 if (config
->neighbour
[i
].update_source
.s_addr
!= INADDR_ANY
)
1005 cli_print(cli
, " neighbour %s update-source %s",
1006 config
->neighbour
[i
].name
,
1007 inet_ntoa(config
->neighbour
[i
].update_source
));
1012 cli_print(cli
, "# Filters");
1013 for (i
= 0; i
< MAXFILTER
; i
++)
1015 ip_filter_rulet
*rules
;
1016 if (!*ip_filters
[i
].name
)
1019 cli_print(cli
, "ip access-list %s %s",
1020 ip_filters
[i
].extended
? "extended" : "standard",
1021 ip_filters
[i
].name
);
1023 rules
= ip_filters
[i
].rules
;
1024 while (rules
->action
)
1025 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1028 cli_print(cli
, "# end");
1032 static int cmd_show_radius(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1034 int i
, free
= 0, used
= 0, show_all
= 0;
1046 if (CLI_HELP_REQUESTED
)
1049 return cli_arg_help(cli
, 1, NULL
);
1051 return cli_arg_help(cli
, 1,
1052 "all", "Show all RADIUS sessions, including unused",
1056 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1060 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1063 for (i
= 1; i
< MAXRADIUS
; i
++)
1065 if (radius
[i
].state
== RADIUSNULL
)
1070 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1072 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1076 states
[radius
[i
].state
],
1082 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1087 static int cmd_show_plugins(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1091 if (CLI_HELP_REQUESTED
)
1092 return CLI_HELP_NO_ARGS
;
1094 cli_print(cli
, "Plugins currently loaded:");
1095 for (i
= 0; i
< MAXPLUGINS
; i
++)
1096 if (*config
->plugins
[i
])
1097 cli_print(cli
, " %s", config
->plugins
[i
]);
1102 static int cmd_show_throttle(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1106 if (CLI_HELP_REQUESTED
)
1107 return CLI_HELP_NO_ARGS
;
1109 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1118 for (i
= 0; i
< MAXSESSION
; i
++)
1120 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1121 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1125 session
[i
].throttle_in
,
1126 session
[i
].throttle_out
,
1128 session
[i
].tbf_out
);
1134 static int cmd_show_banana(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1136 if (CLI_HELP_REQUESTED
)
1137 return CLI_HELP_NO_ARGS
;
1139 cli_print(cli
, " _\n"
1145 " ( \\ `. `-. _,.-:\\\n"
1146 " \\ \\ `. `-._ __..--' ,-';/\n"
1147 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1148 " `. `. `-._ __..--' ,' /\n"
1149 " `. `-_ ``--..'' _.-' ,'\n"
1150 " `-_ `-.___ __,--' ,'\n"
1151 " `-.__ `----\"\"\" __.-'\n"
1152 "hh `--..____..--'");
1157 static int cmd_drop_user(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1162 if (CLI_HELP_REQUESTED
)
1163 return cli_arg_help(cli
, argc
> 1,
1164 "USER", "Username of session to drop", NULL
);
1166 if (!config
->cluster_iam_master
)
1168 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1169 fmtaddr(config
->cluster_master_address
, 0));
1176 cli_error(cli
, "Specify a user to drop");
1180 for (i
= 0; i
< argc
; i
++)
1182 if (!(s
= sessionbyuser(argv
[i
])))
1184 cli_error(cli
, "User %s is not connected", argv
[i
]);
1188 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1190 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1191 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1198 static int cmd_drop_tunnel(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1203 if (CLI_HELP_REQUESTED
)
1204 return cli_arg_help(cli
, argc
> 1,
1205 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1207 if (!config
->cluster_iam_master
)
1209 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1210 fmtaddr(config
->cluster_master_address
, 0));
1217 cli_error(cli
, "Specify a tunnel to drop");
1221 for (i
= 0; i
< argc
; i
++)
1223 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1225 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1231 cli_error(cli
, "Tunnel %d is not connected", t
);
1237 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1241 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1242 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1248 static int cmd_drop_session(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1253 if (CLI_HELP_REQUESTED
)
1254 return cli_arg_help(cli
, argc
> 1,
1255 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1257 if (!config
->cluster_iam_master
)
1259 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1260 fmtaddr(config
->cluster_master_address
, 0));
1267 cli_error(cli
, "Specify a session id to drop");
1271 for (i
= 0; i
< argc
; i
++)
1273 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1275 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1279 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1281 cli_print(cli
, "Dropping session %d", s
);
1282 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1284 else if (session
[s
].forwardtosession
&& session
[s
].opened
&& !session
[s
].die
)
1286 cli_print(cli
, "Dropping session %d", s
);
1287 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1291 cli_error(cli
, "Session %d is not active.", s
);
1298 static int cmd_snoop(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1304 if (CLI_HELP_REQUESTED
)
1309 return cli_arg_help(cli
, 0,
1310 "USER", "Username of session to snoop", NULL
);
1313 return cli_arg_help(cli
, 0,
1314 "A.B.C.D", "IP address of snoop destination", NULL
);
1317 return cli_arg_help(cli
, 0,
1318 "N", "Port of snoop destination", NULL
);
1322 return cli_arg_help(cli
, 1, NULL
);
1329 if (!config
->cluster_iam_master
)
1331 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1332 fmtaddr(config
->cluster_master_address
, 0));
1339 cli_error(cli
, "Specify username, ip and port");
1343 if (!(s
= sessionbyuser(argv
[0])))
1345 cli_error(cli
, "User %s is not connected", argv
[0]);
1349 ip
= inet_addr(argv
[1]);
1350 if (!ip
|| ip
== INADDR_NONE
)
1352 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1356 port
= atoi(argv
[2]);
1359 cli_error(cli
, "Invalid port %s", argv
[2]);
1363 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1364 cli_session_actions
[s
].snoop_ip
= ip
;
1365 cli_session_actions
[s
].snoop_port
= port
;
1366 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1371 static int cmd_no_snoop(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1376 if (CLI_HELP_REQUESTED
)
1377 return cli_arg_help(cli
, argc
> 1,
1378 "USER", "Username of session to unsnoop", NULL
);
1380 if (!config
->cluster_iam_master
)
1382 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1383 fmtaddr(config
->cluster_master_address
, 0));
1390 cli_error(cli
, "Specify a user to unsnoop");
1394 for (i
= 0; i
< argc
; i
++)
1396 if (!(s
= sessionbyuser(argv
[i
])))
1398 cli_error(cli
, "User %s is not connected", argv
[i
]);
1402 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1403 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1409 static int cmd_throttle(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1416 throttle USER - throttle in/out to default rate
1417 throttle USER RATE - throttle in/out to default rate
1418 throttle USER in RATE - throttle input only
1419 throttle USER out RATE - throttle output only
1420 throttle USER in RATE out RATE - throttle both
1423 if (CLI_HELP_REQUESTED
)
1428 return cli_arg_help(cli
, 0,
1429 "USER", "Username of session to throttle", NULL
);
1432 return cli_arg_help(cli
, 1,
1433 "RATE", "Rate in kbps (in and out)",
1434 "in", "Select incoming rate",
1435 "out", "Select outgoing rate", NULL
);
1438 return cli_arg_help(cli
, 1,
1439 "in", "Select incoming rate",
1440 "out", "Select outgoing rate", NULL
);
1443 if (isdigit(argv
[1][0]))
1444 return cli_arg_help(cli
, 1, NULL
);
1447 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1450 return cli_arg_help(cli
, argc
> 1, NULL
);
1454 if (!config
->cluster_iam_master
)
1456 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1457 fmtaddr(config
->cluster_master_address
, 0));
1464 cli_error(cli
, "Specify a user to throttle");
1468 if (!(s
= sessionbyuser(argv
[0])))
1470 cli_error(cli
, "User %s is not connected", argv
[0]);
1476 rate_in
= rate_out
= config
->rl_rate
;
1480 rate_in
= rate_out
= atoi(argv
[1]);
1483 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1487 else if (argc
== 3 || argc
== 5)
1490 for (i
= 1; i
< argc
- 1; i
+= 2)
1493 if (MATCH("in", argv
[i
]))
1494 r
= rate_in
= atoi(argv
[i
+1]);
1495 else if (MATCH("out", argv
[i
]))
1496 r
= rate_out
= atoi(argv
[i
+1]);
1500 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1507 cli_error(cli
, "Invalid arguments");
1511 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1513 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1517 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1518 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1519 cli_session_actions
[s
].throttle_in
= rate_in
;
1521 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1522 cli_session_actions
[s
].throttle_out
= rate_out
;
1524 if (cli_session_actions
[s
].throttle_in
== -1 &&
1525 cli_session_actions
[s
].throttle_out
== -1)
1527 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1531 cli_print(cli
, "Throttling user %s", argv
[0]);
1532 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1537 static int cmd_no_throttle(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1542 if (CLI_HELP_REQUESTED
)
1543 return cli_arg_help(cli
, argc
> 1,
1544 "USER", "Username of session to unthrottle", NULL
);
1546 if (!config
->cluster_iam_master
)
1548 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1549 fmtaddr(config
->cluster_master_address
, 0));
1556 cli_error(cli
, "Specify a user to unthrottle");
1560 for (i
= 0; i
< argc
; i
++)
1562 if (!(s
= sessionbyuser(argv
[i
])))
1564 cli_error(cli
, "User %s is not connected", argv
[i
]);
1568 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1570 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1571 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1575 cli_error(cli
, "User %s not throttled", argv
[i
]);
1582 static int cmd_debug(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1586 if (CLI_HELP_REQUESTED
)
1587 return cli_arg_help(cli
, 1,
1588 "all", "Enable debugging for all except \"data\"",
1589 "critical", "", // FIXME: add descriptions
1599 char *p
= (char *) &debug_flags
;
1600 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1604 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1605 (debug_flags
.critical
) ? " critical" : "",
1606 (debug_flags
.error
) ? " error" : "",
1607 (debug_flags
.warning
) ? " warning" : "",
1608 (debug_flags
.info
) ? " info" : "",
1609 (debug_flags
.calls
) ? " calls" : "",
1610 (debug_flags
.data
) ? " data" : "");
1616 cli_print(cli
, "Debugging off");
1620 for (i
= 0; i
< argc
; i
++)
1622 int len
= strlen(argv
[i
]);
1624 if (argv
[i
][0] == 'c' && len
< 2)
1625 len
= 2; /* distinguish [cr]itical from [ca]lls */
1627 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1628 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1629 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1630 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1631 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1632 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1633 if (!strncmp("all", argv
[i
], len
))
1635 memset(&debug_flags
, 1, sizeof(debug_flags
));
1636 debug_flags
.data
= 0;
1640 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1646 static int cmd_no_debug(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1650 if (CLI_HELP_REQUESTED
)
1651 return cli_arg_help(cli
, 1,
1652 "all", "Disable all debugging",
1653 "critical", "", // FIXME: add descriptions
1663 memset(&debug_flags
, 0, sizeof(debug_flags
));
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
= 0; continue; }
1675 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1676 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1677 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1678 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1679 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1680 if (!strncmp("all", argv
[i
], len
))
1682 memset(&debug_flags
, 0, sizeof(debug_flags
));
1686 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1692 static int cmd_load_plugin(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1694 int i
, firstfree
= 0;
1696 if (CLI_HELP_REQUESTED
)
1697 return cli_arg_help(cli
, argc
> 1,
1698 "PLUGIN", "Name of plugin to load", NULL
);
1702 cli_error(cli
, "Specify a plugin to load");
1706 for (i
= 0; i
< MAXPLUGINS
; i
++)
1708 if (!*config
->plugins
[i
] && !firstfree
)
1710 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1712 cli_error(cli
, "Plugin is already loaded");
1719 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1720 config
->reload_config
= 1;
1721 cli_print(cli
, "Loading plugin %s", argv
[0]);
1727 static int cmd_remove_plugin(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1731 if (CLI_HELP_REQUESTED
)
1732 return cli_arg_help(cli
, argc
> 1,
1733 "PLUGIN", "Name of plugin to unload", NULL
);
1737 cli_error(cli
, "Specify a plugin to remove");
1741 for (i
= 0; i
< MAXPLUGINS
; i
++)
1743 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1745 config
->reload_config
= 1;
1746 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1751 cli_error(cli
, "Plugin is not loaded");
1755 static char *duration(time_t secs
)
1757 static char *buf
= NULL
;
1760 if (!buf
) buf
= calloc(64, 1);
1764 int days
= secs
/ 86400;
1765 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1771 int mins
= secs
/ 60;
1772 int hrs
= mins
/ 60;
1775 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1777 else if (secs
>= 60)
1779 int mins
= secs
/ 60;
1780 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1783 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1788 static int cmd_uptime(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1791 char buf
[100], *p
= buf
, *loads
[3];
1792 int i
, num_sessions
= 0;
1794 if (CLI_HELP_REQUESTED
)
1795 return CLI_HELP_NO_ARGS
;
1797 fh
= fopen("/proc/loadavg", "r");
1798 p
= fgets(buf
, 100, fh
);
1801 for (i
= 0; i
< 3; i
++)
1802 loads
[i
] = strdup(strsep(&p
, " "));
1805 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1807 for (i
= 1; i
< MAXSESSION
; i
++)
1808 if (session
[i
].opened
) num_sessions
++;
1810 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1812 duration(time_now
- config
->start_time
),
1814 loads
[0], loads
[1], loads
[2]
1816 for (i
= 0; i
< 3; i
++)
1817 if (loads
[i
]) free(loads
[i
]);
1819 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1824 static int cmd_set(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1828 if (CLI_HELP_REQUESTED
)
1834 int len
= strlen(argv
[0])-1;
1835 for (i
= 0; config_values
[i
].key
; i
++)
1836 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1837 cli_error(cli
, " %s", config_values
[i
].key
);
1843 return cli_arg_help(cli
, 0,
1844 "VALUE", "Value for variable", NULL
);
1848 return cli_arg_help(cli
, 1, NULL
);
1857 cli_error(cli
, "Specify variable and value");
1861 for (i
= 0; config_values
[i
].key
; i
++)
1863 void *value
= ((void *) config
) + config_values
[i
].offset
;
1864 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1866 // Found a value to set
1867 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1868 switch (config_values
[i
].type
)
1871 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1874 *(int *) value
= atoi(argv
[1]);
1877 *(unsigned long *) value
= atol(argv
[1]);
1880 *(short *) value
= atoi(argv
[1]);
1883 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1886 inet_pton(AF_INET6
, argv
[1], value
);
1889 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1895 cli_error(cli
, "Unknown variable type");
1898 config
->reload_config
= 1;
1903 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1907 int regular_stuff(struct cli_def
*cli
)
1913 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1915 char *m
= ringbuffer
->buffer
[i
].message
;
1921 switch (ringbuffer
->buffer
[i
].level
)
1923 case 0: show
= debug_flags
.critical
; break;
1924 case 1: show
= debug_flags
.error
; break;
1925 case 2: show
= debug_flags
.warning
; break;
1926 case 3: show
= debug_flags
.info
; break;
1927 case 4: show
= debug_flags
.calls
; break;
1928 case 5: show
= debug_flags
.data
; break;
1931 if (!show
) continue;
1933 if (!(p
= strchr(m
, '\n')))
1936 cli_error(cli
, "\r%s-%u-%u %.*s",
1937 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1938 ringbuffer
->buffer
[i
].tunnel
,
1939 ringbuffer
->buffer
[i
].session
,
1945 debug_rb_tail
= ringbuffer
->tail
;
1953 static int cmd_router_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1957 if (CLI_HELP_REQUESTED
)
1958 return cli_arg_help(cli
, argc
> 1,
1959 "<1-65535>", "Autonomous system number", NULL
);
1961 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1963 cli_error(cli
, "Invalid autonomous system number");
1967 if (bgp_configured
&& as
!= config
->as_number
)
1969 cli_error(cli
, "Can't change local AS on a running system");
1973 config
->as_number
= as
;
1974 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1979 static int find_bgp_neighbour(char const *name
)
1984 in_addr_t addrs
[4] = { 0 };
1987 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1990 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1991 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1993 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1995 if (!config
->neighbour
[i
].name
[0])
1997 if (new == -1) new = i
;
2001 if (!strcmp(name
, config
->neighbour
[i
].name
))
2004 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
2007 for (a
= h
->h_addr_list
; *a
; a
++)
2010 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
2011 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2019 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2025 if (CLI_HELP_REQUESTED
)
2030 return cli_arg_help(cli
, 0,
2031 "A.B.C.D", "BGP neighbour address",
2032 "NAME", "BGP neighbour name",
2036 return cli_arg_help(cli
, 0,
2037 "remote-as", "Set remote autonomous system number",
2038 "timers", "Set timers",
2039 "update-source", "Set source address to use for the BGP session",
2043 if (MATCH("remote-as", argv
[1]))
2044 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2046 if (MATCH("timers", argv
[1]))
2049 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2052 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2054 if (argc
== 5 && !argv
[4][1])
2055 return cli_arg_help(cli
, 1, NULL
);
2058 if (MATCH("update-source", argv
[1]))
2059 return cli_arg_help(cli
, argc
> 3, "A.B.C.D", "Source IP address", NULL
);
2067 cli_error(cli
, "Invalid arguments");
2071 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2073 cli_error(cli
, "Invalid neighbour");
2079 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2083 if (MATCH("remote-as", argv
[1]))
2085 int as
= atoi(argv
[2]);
2086 if (as
< 0 || as
> 65535)
2088 cli_error(cli
, "Invalid autonomous system number");
2092 if (!config
->neighbour
[i
].name
[0])
2094 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2095 config
->neighbour
[i
].keepalive
= -1;
2096 config
->neighbour
[i
].hold
= -1;
2097 config
->neighbour
[i
].update_source
.s_addr
= INADDR_ANY
;
2100 config
->neighbour
[i
].as
= as
;
2104 if (MATCH("update-source", argv
[1]))
2106 struct in_addr addr
;
2108 if (!config
->neighbour
[i
].name
[0])
2110 cli_error(cli
, "Specify remote-as first");
2114 if (!inet_aton(argv
[2], &addr
))
2116 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[2]);
2120 config
->neighbour
[i
].update_source
= addr
;
2124 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2126 cli_error(cli
, "Invalid arguments");
2130 if (!config
->neighbour
[i
].name
[0])
2132 cli_error(cli
, "Specify remote-as first");
2136 keepalive
= atoi(argv
[2]);
2137 hold
= atoi(argv
[3]);
2139 if (keepalive
< 1 || keepalive
> 65535)
2141 cli_error(cli
, "Invalid keepalive time");
2145 if (hold
< 3 || hold
> 65535)
2147 cli_error(cli
, "Invalid hold time");
2151 if (keepalive
== BGP_KEEPALIVE_TIME
)
2152 keepalive
= -1; // using default value
2154 if (hold
== BGP_HOLD_TIME
)
2157 config
->neighbour
[i
].keepalive
= keepalive
;
2158 config
->neighbour
[i
].hold
= hold
;
2163 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2167 if (CLI_HELP_REQUESTED
)
2168 return cli_arg_help(cli
, argc
> 0,
2169 "A.B.C.D", "BGP neighbour address",
2170 "NAME", "BGP neighbour name",
2175 cli_error(cli
, "Specify a BGP neighbour");
2179 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2181 cli_error(cli
, "Invalid neighbour");
2185 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2187 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2191 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2195 static int cmd_show_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2201 if (!bgp_configured
)
2204 if (CLI_HELP_REQUESTED
)
2205 return cli_arg_help(cli
, 1,
2206 "A.B.C.D", "BGP neighbour address",
2207 "NAME", "BGP neighbour name",
2210 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2211 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2215 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2217 if (!*bgp_peers
[i
].name
)
2220 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2221 if (argc
&& strcmp(addr
, argv
[0]) &&
2222 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2227 cli_print(cli
, " ");
2228 cli_print(cli
, "Peer AS Address "
2229 "State Retries Retry in Route Pend Timers");
2230 cli_print(cli
, "------------------ ----- --------------- "
2231 "----------- ------- -------- ----- ---- ---------");
2234 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2238 bgp_state_str(bgp_peers
[i
].state
),
2239 bgp_peers
[i
].retry_count
,
2240 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2241 bgp_peers
[i
].routing
? "yes" : "no",
2242 bgp_peers
[i
].update_routes
? "yes" : "no",
2243 bgp_peers
[i
].keepalive
,
2250 static int cmd_suspend_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2255 if (!bgp_configured
)
2258 if (CLI_HELP_REQUESTED
)
2259 return cli_arg_help(cli
, 1,
2260 "A.B.C.D", "BGP neighbour address",
2261 "NAME", "BGP neighbour name",
2264 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2266 if (bgp_peers
[i
].state
!= Established
)
2269 if (!bgp_peers
[i
].routing
)
2272 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2273 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2276 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2277 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2283 static int cmd_no_suspend_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2288 if (!bgp_configured
)
2291 if (CLI_HELP_REQUESTED
)
2292 return cli_arg_help(cli
, 1,
2293 "A.B.C.D", "BGP neighbour address",
2294 "NAME", "BGP neighbour name",
2297 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2299 if (bgp_peers
[i
].state
!= Established
)
2302 if (bgp_peers
[i
].routing
)
2305 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2306 if (argc
&& strcmp(addr
, argv
[0]) &&
2307 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2310 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2311 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2317 static int cmd_restart_bgp(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2322 if (!bgp_configured
)
2325 if (CLI_HELP_REQUESTED
)
2326 return cli_arg_help(cli
, 1,
2327 "A.B.C.D", "BGP neighbour address",
2328 "NAME", "BGP neighbour name",
2331 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2333 if (!*bgp_peers
[i
].name
)
2336 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2337 if (argc
&& strcmp(addr
, argv
[0]) &&
2338 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2341 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2342 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2350 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2354 if (CLI_HELP_REQUESTED
)
2359 return cli_arg_help(cli
, 0,
2360 "standard", "Standard syntax",
2361 "extended", "Extended syntax",
2365 return cli_arg_help(cli
, argv
[1][1],
2366 "NAME", "Access-list name",
2370 if (argc
== 3 && !argv
[2][1])
2371 return cli_arg_help(cli
, 1, NULL
);
2379 cli_error(cli
, "Specify access-list type and name");
2383 if (MATCH("standard", argv
[0]))
2385 else if (MATCH("extended", argv
[0]))
2389 cli_error(cli
, "Invalid access-list type");
2393 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2394 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2396 cli_error(cli
, "Invalid access-list name");
2400 filt
= find_filter(argv
[1], strlen(argv
[1]));
2405 cli_error(cli
, "Too many access-lists");
2410 if (!*ip_filters
[filt
].name
)
2412 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2413 strcpy(ip_filters
[filt
].name
, argv
[1]);
2414 ip_filters
[filt
].extended
= extended
;
2416 else if (ip_filters
[filt
].extended
!= extended
)
2418 cli_error(cli
, "Access-list is %s",
2419 ip_filters
[filt
].extended
? "extended" : "standard");
2424 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2428 if (filt
< 0 || !*ip_filters
[filt
].name
)
2430 cli_error(cli
, "Access-list not defined");
2435 if (ip_filters
[filt
].used
)
2437 cli_error(cli
, "Access-list in use");
2441 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2445 static int cmd_ip_access_list(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2447 return access_list(cli
, argv
, argc
, 1);
2450 static int cmd_no_ip_access_list(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2452 return access_list(cli
, argv
, argc
, 0);
2455 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2457 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2458 return sprintf(buf
, " any");
2460 if (wild
== INADDR_ANY
)
2461 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2463 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2466 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2470 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2471 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2472 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2473 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2474 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2480 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2482 static char buf
[256];
2485 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2488 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2489 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2492 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2496 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2497 p
+= show_ports(p
, &rule
->src_ports
);
2499 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2500 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2501 p
+= show_ports(p
, &rule
->dst_ports
);
2503 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2505 switch (rule
->tcp_flag_op
)
2507 case FILTER_FLAG_OP_EST
:
2508 p
+= sprintf(p
, " established");
2511 case FILTER_FLAG_OP_ANY
:
2512 case FILTER_FLAG_OP_ALL
:
2513 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2514 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2515 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2516 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2517 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2518 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2519 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2520 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2521 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2522 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2523 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2524 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2525 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2531 p
+= sprintf(p
, " fragments");
2536 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2538 static ip_filter_rulet rule
;
2539 struct in_addr addr
;
2543 if (CLI_HELP_REQUESTED
)
2547 cli_arg_help(cli
, 0,
2548 "ip", "Match IP packets",
2549 "tcp", "Match TCP packets",
2550 "udp", "Match UDP packets",
2556 // *sigh*, too darned complex
2557 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2563 cli_error(cli
, "Specify rule details");
2567 memset(&rule
, 0, sizeof(rule
));
2568 rule
.action
= (command
[0] == 'p')
2569 ? FILTER_ACTION_PERMIT
2570 : FILTER_ACTION_DENY
;
2572 if (MATCH("ip", argv
[0]))
2573 rule
.proto
= IPPROTO_IP
;
2574 else if (MATCH("udp", argv
[0]))
2575 rule
.proto
= IPPROTO_UDP
;
2576 else if (MATCH("tcp", argv
[0]))
2577 rule
.proto
= IPPROTO_TCP
;
2580 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2584 for (a
= 1, i
= 0; i
< 2; i
++)
2588 ip_filter_portt
*port
;
2593 wild
= &rule
.src_wild
;
2594 port
= &rule
.src_ports
;
2599 wild
= &rule
.dst_wild
;
2600 port
= &rule
.dst_ports
;
2603 cli_error(cli
, "Specify destination");
2608 if (MATCH("any", argv
[a
]))
2611 *wild
= INADDR_BROADCAST
;
2614 else if (MATCH("host", argv
[a
]))
2618 cli_error(cli
, "Specify host ip address");
2622 if (!inet_aton(argv
[a
], &addr
))
2624 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2636 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2640 if (!inet_aton(argv
[a
], &addr
))
2642 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2648 if (!inet_aton(argv
[++a
], &addr
))
2650 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2654 *wild
= addr
.s_addr
;
2658 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2662 if (MATCH("eq", argv
[a
]))
2663 port
->op
= FILTER_PORT_OP_EQ
;
2664 else if (MATCH("neq", argv
[a
]))
2665 port
->op
= FILTER_PORT_OP_NEQ
;
2666 else if (MATCH("gt", argv
[a
]))
2667 port
->op
= FILTER_PORT_OP_GT
;
2668 else if (MATCH("lt", argv
[a
]))
2669 port
->op
= FILTER_PORT_OP_LT
;
2670 else if (MATCH("range", argv
[a
]))
2671 port
->op
= FILTER_PORT_OP_RANGE
;
2678 cli_error(cli
, "Specify port");
2682 if (!(port
->port
= atoi(argv
[a
])))
2684 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2689 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2694 cli_error(cli
, "Specify port");
2698 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2700 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2707 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2709 if (MATCH("established", argv
[a
]))
2711 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2714 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2715 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2717 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2718 ? FILTER_FLAG_OP_ANY
2719 : FILTER_FLAG_OP_ALL
;
2723 cli_error(cli
, "Specify tcp flags");
2727 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2731 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2733 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2734 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2735 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2736 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2737 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2738 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2741 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2750 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2752 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2754 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2764 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2771 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2773 static ip_filter_rulet rule
;
2774 struct in_addr addr
;
2776 if (CLI_HELP_REQUESTED
)
2780 cli_arg_help(cli
, argv
[0][1],
2781 "A.B.C.D", "Source address",
2782 "any", "Any source address",
2783 "host", "Source host",
2789 if (MATCH("any", argv
[0]))
2791 if (argc
== 2 && !argv
[1][1])
2792 cli_arg_help(cli
, 1, NULL
);
2794 else if (MATCH("host", argv
[0]))
2798 cli_arg_help(cli
, argv
[1][1],
2799 "A.B.C.D", "Host address",
2802 else if (argc
== 3 && !argv
[2][1])
2803 cli_arg_help(cli
, 1, NULL
);
2809 cli_arg_help(cli
, 1,
2810 "A.B.C.D", "Wildcard bits",
2813 else if (argc
== 3 && !argv
[2][1])
2814 cli_arg_help(cli
, 1, NULL
);
2822 cli_error(cli
, "Specify rule details");
2826 memset(&rule
, 0, sizeof(rule
));
2827 rule
.action
= (command
[0] == 'p')
2828 ? FILTER_ACTION_PERMIT
2829 : FILTER_ACTION_DENY
;
2831 rule
.proto
= IPPROTO_IP
;
2832 if (MATCH("any", argv
[0]))
2834 rule
.src_ip
= INADDR_ANY
;
2835 rule
.src_wild
= INADDR_BROADCAST
;
2837 else if (MATCH("host", argv
[0]))
2841 cli_error(cli
, "Specify host ip address");
2845 if (!inet_aton(argv
[1], &addr
))
2847 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2851 rule
.src_ip
= addr
.s_addr
;
2852 rule
.src_wild
= INADDR_ANY
;
2858 cli_error(cli
, "Specify source ip address and wildcard");
2862 if (!inet_aton(argv
[0], &addr
))
2864 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2868 rule
.src_ip
= addr
.s_addr
;
2872 if (!inet_aton(argv
[1], &addr
))
2874 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2878 rule
.src_wild
= addr
.s_addr
;
2881 rule
.src_wild
= INADDR_ANY
;
2887 static int cmd_ip_access_list_rule(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2890 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2891 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2892 : access_list_rule_std(cli
, command
, argv
, argc
);
2897 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2899 if (!ip_filters
[filt
].rules
[i
].action
)
2901 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2905 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2909 cli_error(cli
, "Too many rules");
2913 static int cmd_filter(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
2918 /* filter USER {in|out} FILTER ... */
2919 if (CLI_HELP_REQUESTED
)
2924 return cli_arg_help(cli
, 0,
2925 "USER", "Username of session to filter", NULL
);
2929 return cli_arg_help(cli
, 0,
2930 "in", "Set incoming filter",
2931 "out", "Set outgoing filter", NULL
);
2935 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2936 "NAME", "Filter name", NULL
);
2939 return cli_arg_help(cli
, argc
> 1, NULL
);
2943 if (!config
->cluster_iam_master
)
2945 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2946 fmtaddr(config
->cluster_master_address
, 0));
2951 if (argc
!= 3 && argc
!= 5)
2953 cli_error(cli
, "Specify a user and filters");
2957 if (!(s
= sessionbyuser(argv
[0])))
2959 cli_error(cli
, "User %s is not connected", argv
[0]);
2963 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2964 for (i
= 1; i
< argc
; i
+= 2)
2969 if (MATCH("in", argv
[i
]))
2971 if (session
[s
].filter_in
)
2973 cli_error(cli
, "Input already filtered");
2976 f
= &cli_session_actions
[s
].filter_in
;
2978 else if (MATCH("out", argv
[i
]))
2980 if (session
[s
].filter_out
)
2982 cli_error(cli
, "Output already filtered");
2985 f
= &cli_session_actions
[s
].filter_out
;
2989 cli_error(cli
, "Invalid filter specification");
2993 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
2994 if (v
< 0 || !*ip_filters
[v
].name
)
2996 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
3003 cli_print(cli
, "Filtering user %s", argv
[0]);
3004 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
3009 static int cmd_no_filter(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
3014 if (CLI_HELP_REQUESTED
)
3015 return cli_arg_help(cli
, argc
> 1,
3016 "USER", "Username of session to remove filters from", NULL
);
3018 if (!config
->cluster_iam_master
)
3020 cli_error(cli
, "Can't do this on a slave. Do it on %s",
3021 fmtaddr(config
->cluster_master_address
, 0));
3028 cli_error(cli
, "Specify a user to remove filters from");
3032 for (i
= 0; i
< argc
; i
++)
3034 if (!(s
= sessionbyuser(argv
[i
])))
3036 cli_error(cli
, "User %s is not connected", argv
[i
]);
3040 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3042 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3043 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3047 cli_error(cli
, "User %s not filtered", argv
[i
]);
3054 static int cmd_show_access_list(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
3058 if (CLI_HELP_REQUESTED
)
3059 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3063 cli_error(cli
, "Specify a filter name");
3067 for (i
= 0; i
< argc
; i
++)
3069 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
3070 ip_filter_rulet
*rules
;
3072 if (f
< 0 || !*ip_filters
[f
].name
)
3074 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3079 cli_print(cli
, " ");
3081 cli_print(cli
, "%s IP access list %s",
3082 ip_filters
[f
].extended
? "Extended" : "Standard",
3083 ip_filters
[f
].name
);
3085 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3087 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3089 cli_print(cli
, "%s (%u match%s)", r
,
3090 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3092 cli_print(cli
, "%s", r
);
3099 static int cmd_shutdown(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
3101 if (CLI_HELP_REQUESTED
)
3102 return CLI_HELP_NO_ARGS
;
3104 kill(getppid(), SIGQUIT
);
3108 static int cmd_reload(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
3110 if (CLI_HELP_REQUESTED
)
3111 return CLI_HELP_NO_ARGS
;
3113 kill(getppid(), SIGHUP
);
3117 static int cmd_show_group(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
3122 if (CLI_HELP_REQUESTED
)
3123 return cli_arg_help(cli
, 1,
3124 "<1-%d>", MAXGROUPE
-1, "Show specific group by id",
3130 // Show individual group
3131 for (i
= 0; i
< argc
; i
++)
3136 if (g
<= 0 || g
>= MAXGROUPE
)
3138 cli_print(cli
, "Invalid group id \"%s\"", argv
[i
]);
3142 cli_print(cli
, "\r\nGroup %d:", g
);
3143 cli_print(cli
, "\tNb Session:\t\t%d", grpsession
[g
].nbsession
);
3144 cli_print(cli
, "\tNb Routes:\t\t%d", grpsession
[g
].nbroutesgrp
);
3145 cli_print(cli
, "\ttime_changed:\t\t%d\n", grpsession
[g
].time_changed
);
3147 for (i
= 0; i
< grpsession
[g
].nbsession
; i
++)
3149 if ((s
= grpsession
[g
].sesslist
[i
].sid
))
3151 cli_print(cli
, "\tSession:\t%d\tTx Rate:%d Kbps\t\t\tweight:\t%d",
3153 grpsession
[g
].sesslist
[i
].tx_rate
/(1024/8),
3154 grpsession
[g
].sesslist
[i
].weight
);
3158 for (i
= 0; i
< grpsession
[g
].nbroutesgrp
; i
++)
3160 if (grpsession
[g
].route
[i
].ip
!= 0)
3162 cli_print(cli
, "\tRoute:\t%s/%d", fmtaddr(htonl(grpsession
[g
].route
[i
].ip
), 0), grpsession
[g
].route
[i
].prefixlen
);
3170 cli_print(cli
, "%5s %7s %9s %12s",
3176 for (g
= gnextgrpid
; g
!= 0; g
= grpsession
[g
].prev
)
3178 cli_print(cli
, "%5d %7d %9d %12d",
3180 grpsession
[g
].nbsession
,
3181 grpsession
[g
].nbroutesgrp
,
3182 grpsession
[g
].time_changed
);
3188 static int cmd_setforward(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
3192 if (CLI_HELP_REQUESTED
)
3197 return cli_arg_help(cli
, 0,
3198 "MASK", "Users mask to forward (ex: myISP@operator.com)", NULL
);
3201 return cli_arg_help(cli
, 0,
3202 "IP", "IP of the remote LNS(ex: 64.64.64.64)", NULL
);
3205 return cli_arg_help(cli
, 0,
3206 "PORT", "Port of the remote LNS (ex: 1701)", NULL
);
3209 return cli_arg_help(cli
, 0,
3210 "SECRET", "l2tp secret of the remote LNS (ex: mysecretpsw)", NULL
);
3213 return cli_arg_help(cli
, argc
> 1, NULL
);
3219 cli_error(cli
, "Specify variable and value");
3223 // lac_addremotelns(mask, IP_RemoteLNS, Port_RemoteLNS, SecretRemoteLNS)
3224 ret
= lac_addremotelns(argv
[0], argv
[1], argv
[2], argv
[3]);
3228 cli_print(cli
, "setforward %s %s %s %s", argv
[0], argv
[1], argv
[2], argv
[3]);
3230 cli_print(cli
, "%s Updated, the tunnel must be dropped", argv
[0]);
3233 cli_error(cli
, "ERROR setforward %s %s %s %s", argv
[0], argv
[1], argv
[2], argv
[3]);
3238 static int cmd_show_rmtlnsconf(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
3243 if (CLI_HELP_REQUESTED
)
3245 return cli_arg_help(cli
, 0, "remotelns-conf", "Show a list of remote LNS configurations", NULL
);
3248 for (idrlns
= 0; idrlns
< MAXRLNSTUNNEL
; idrlns
++)
3250 if (lac_cli_show_remotelns(idrlns
, strdisp
) != 0)
3251 cli_print(cli
, "%s", strdisp
);