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>
27 #include "constants.h"
39 extern tunnelt
*tunnel
;
40 extern bundlet
*bundle
;
41 extern sessiont
*session
;
42 extern radiust
*radius
;
43 extern ippoolt
*ip_address_pool
;
44 extern struct Tstats
*_statistics
;
45 static struct cli_def
*cli
= NULL
;
46 extern configt
*config
;
47 extern config_descriptt config_values
[];
49 extern struct Tringbuffer
*ringbuffer
;
51 extern struct cli_session_actions
*cli_session_actions
;
52 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
53 extern tbft
*filter_list
;
54 extern ip_filtert
*ip_filters
;
68 static int debug_rb_tail
;
69 static char *debug_levels
[] = {
80 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_group(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
102 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
103 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
104 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
105 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
106 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_setforward(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 static int cmd_show_rmtlnsconf(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
112 static int regular_stuff(struct cli_def
*cli
);
115 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
116 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 #endif /* STATISTICS */
120 #define MODE_CONFIG_BGP 8
121 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
122 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
123 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
124 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
125 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
126 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
127 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
130 #define MODE_CONFIG_NACL 9
131 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
132 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
133 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
134 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
135 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
136 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
138 /* match if b is a substr of a */
139 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
145 struct cli_command
*c
;
146 struct cli_command
*c2
;
150 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
151 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
153 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
155 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
156 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
157 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
158 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
159 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
160 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
162 cli_register_command(cli
, c
, "remotelns-conf", cmd_show_rmtlnsconf
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show a list of remote LNS configuration");
164 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
165 cli_register_command(cli
, c
, "group", cmd_show_group
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of groups or details for a single group");
166 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
167 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
168 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
169 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");
170 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
171 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
173 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
174 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
175 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
178 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
180 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
181 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
184 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
185 cli_register_command(cli
, NULL
, "shutdown", cmd_shutdown
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Shutdown l2tpns daemon and exit");
186 cli_register_command(cli
, NULL
, "reload", cmd_reload
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Reload configuration");
188 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
189 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
190 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
192 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
193 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
194 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
195 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
198 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
199 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
202 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
203 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
204 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
205 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
206 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
209 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
210 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
212 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
213 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
215 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
216 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
218 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
220 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
221 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
224 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
225 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
226 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
227 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
229 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
230 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
232 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
233 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
235 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
238 cli_register_command(cli
, NULL
, "setforward", cmd_setforward
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set the Remote LNS Forward");
241 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
242 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
244 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
245 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
247 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
248 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
249 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
251 // Enable regular processing
252 cli_regular(cli
, regular_stuff
);
254 if (!(f
= fopen(CLIUSERS
, "r")))
256 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
260 while (fgets(buf
, 4096, f
))
263 if (*buf
== '#') continue;
264 if ((p
= strchr(buf
, '\r'))) *p
= 0;
265 if ((p
= strchr(buf
, '\n'))) *p
= 0;
267 if (!(p
= strchr((char *)buf
, ':'))) continue;
269 if (!strcmp(buf
, "enable"))
271 cli_allow_enable(cli
, p
);
272 LOG(3, 0, 0, "Setting enable password\n");
276 cli_allow_user(cli
, buf
, p
);
277 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
284 void cli_init_complete(char *hostname
)
287 struct sockaddr_in addr
;
289 if (hostname
&& *hostname
)
290 cli_set_hostname(cli
, hostname
);
292 cli_set_hostname(cli
, "l2tpns");
294 memset(&addr
, 0, sizeof(addr
));
295 clifd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
296 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
299 // Set cli fd as non-blocking
300 flags
= fcntl(clifd
, F_GETFL
, 0);
301 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
303 addr
.sin_family
= AF_INET
;
304 addr
.sin_addr
.s_addr
= config
->cli_bind_address
; /* defaults to INADDR_ANY */
305 addr
.sin_port
= htons(23);
306 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
308 LOG(0, 0, 0, "Error binding cli on port 23: %s\n", strerror(errno
));
313 if (listen(clifd
, 10) < 0)
315 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
322 void cli_do(int sockfd
)
324 int require_auth
= 1;
325 struct sockaddr_in addr
;
326 socklen_t l
= sizeof(addr
);
328 if (fork_and_close()) return;
329 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
331 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
332 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
333 fmtaddr(addr
.sin_addr
.s_addr
, 0));
336 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
340 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
341 if (!cli
->users
) /* paranoia */
343 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
349 /* no username/pass required */
354 debug_rb_tail
= ringbuffer
->tail
;
356 memset(&debug_flags
, 0, sizeof(debug_flags
));
357 debug_flags
.critical
= 1;
359 cli_loop(cli
, sockfd
);
362 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
363 fmtaddr(addr
.sin_addr
.s_addr
, 0));
368 static void cli_print_log(struct cli_def
*cli
, char *string
)
370 LOG(3, 0, 0, "%s\n", string
);
373 void cli_do_file(FILE *fh
)
375 LOG(3, 0, 0, "Reading configuration file\n");
376 cli_print_callback(cli
, cli_print_log
);
377 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
378 cli_print_callback(cli
, NULL
);
381 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
392 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
394 int v
= va_arg(ap
, int);
395 snprintf(buf
, sizeof(buf
), entry
, v
);
401 desc
= va_arg(ap
, char *);
403 cli_error(cli
, " %-20s %s", p
, desc
);
405 cli_error(cli
, " %s", p
);
407 entry
= desc
? va_arg(ap
, char *) : 0;
412 cli_error(cli
, " <cr>");
417 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
421 if (CLI_HELP_REQUESTED
)
422 return cli_arg_help(cli
, 1,
423 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
429 // Show individual session
430 for (i
= 0; i
< argc
; i
++)
432 unsigned int s
, b_in
, b_out
;
434 if (s
<= 0 || s
>= MAXSESSION
)
436 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
439 cli_print(cli
, "\r\nSession %d:", s
);
440 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
441 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
442 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
443 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
444 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
445 switch (session
[s
].ppp
.phase
)
448 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
453 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
454 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
455 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
457 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
458 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
459 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
460 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
461 if (session
[s
].session_timeout
)
463 clockt opened
= session
[s
].opened
;
464 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
465 opened
= bundle
[session
[s
].bundle
].online_time
;
467 cli_print(cli
, "\tSess Timeout:\t%u seconds", session
[s
].session_timeout
- (opened
? abs(time_now
- opened
) : 0));
470 if (session
[s
].idle_timeout
)
471 cli_print(cli
, "\tIdle Timeout:\t%u seconds", session
[s
].idle_timeout
- (session
[s
].last_data
? abs(time_now
- session
[s
].last_data
) : 0));
473 if (session
[s
].timeout
)
475 cli_print(cli
, "\tRemaining time:\t%u",
476 (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
477 ? (unsigned) (session
[s
].timeout
- bundle
[session
[s
].bundle
].online_time
)
478 : (unsigned) (session
[s
].timeout
- (time_now
- session
[s
].opened
)));
481 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
482 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
483 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
484 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
485 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
486 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
487 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
488 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
489 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
490 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
491 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
493 cli_print(cli
, "\tIntercepted:\tno");
495 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
497 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
498 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
499 t
? "YES" : "no", t
? " (" : "",
500 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
502 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
506 b_in
= session
[s
].tbf_in
;
507 b_out
= session
[s
].tbf_out
;
509 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
510 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
511 "ByteSent", "PackSent", "PackDrop", "PackDelay");
514 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
516 (filter_list
[b_in
].next
? "*" : " "),
517 (b_in
< 100 ? "\t" : ""),
518 filter_list
[b_in
].rate
* 8,
519 filter_list
[b_in
].credit
,
520 filter_list
[b_in
].queued
,
521 filter_list
[b_in
].b_queued
,
522 filter_list
[b_in
].p_queued
,
523 filter_list
[b_in
].b_sent
,
524 filter_list
[b_in
].p_sent
,
525 filter_list
[b_in
].p_dropped
,
526 filter_list
[b_in
].p_delayed
);
529 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
531 (filter_list
[b_out
].next
? "*" : " "),
532 (b_out
< 100 ? "\t" : ""),
533 filter_list
[b_out
].rate
* 8,
534 filter_list
[b_out
].credit
,
535 filter_list
[b_out
].queued
,
536 filter_list
[b_out
].b_queued
,
537 filter_list
[b_out
].p_queued
,
538 filter_list
[b_out
].b_sent
,
539 filter_list
[b_out
].p_sent
,
540 filter_list
[b_out
].p_dropped
,
541 filter_list
[b_out
].p_delayed
);
549 cli_print(cli
, "%5s %7s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-18s %s",
551 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-15s %s",
570 "LAC(L)/RLNS(R)/PPPOE(P)",
576 for (i
= 1; i
< MAXSESSION
; i
++)
579 if (!session
[i
].opened
) continue;
580 if (session
[i
].bundle
&& bundle
[session
[i
].bundle
].num_of_links
> 1)
581 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- bundle
[session
[i
].bundle
].online_time
) : 0;
583 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- (time_now
-session
[i
].opened
)) : 0;
585 cli_print(cli
, "%5d %7d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-20s %s",
587 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-20s %s",
591 session
[i
].forwardtosession
,
594 session
[i
].user
[0] ? session
[i
].user
: "*",
595 fmtaddr(htonl(session
[i
].ip
), 0),
596 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
597 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
598 (session
[i
].walled_garden
) ? "Y" : "N",
599 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
600 abs(time_now
- (unsigned long)session
[i
].opened
),
601 (unsigned long)session
[i
].cout
,
602 (unsigned long)session
[i
].cin
,
603 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
604 (unsigned long)(rem_time
),
606 (session
[i
].tunnel
== TUNNEL_ID_PPPOE
)?"(P)":(tunnel
[session
[i
].tunnel
].isremotelns
?"(R)":"(L)"),
608 (session
[i
].tunnel
== TUNNEL_ID_PPPOE
)?"(P)":"(L)",
610 (session
[i
].tunnel
== TUNNEL_ID_PPPOE
)?fmtMacAddr(session
[i
].src_hwaddr
):fmtaddr(htonl(tunnel
[session
[i
].tunnel
].ip
), 1),
611 session
[i
].calling
[0] ? session
[i
].calling
: "*");
616 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
618 int i
, x
, show_all
= 0;
626 if (CLI_HELP_REQUESTED
)
629 return cli_arg_help(cli
, 1,
630 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
633 return cli_arg_help(cli
, 1,
634 "all", "Show all tunnels, including unused",
635 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
642 if (strcmp(argv
[0], "all") == 0)
648 // Show individual tunnel
649 for (i
= 0; i
< argc
; i
++)
654 if (t
<= 0 || t
>= MAXTUNNEL
)
656 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
659 cli_print(cli
, "\r\nTunnel %d:", t
);
660 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
661 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
662 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
663 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
664 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
665 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
666 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
667 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
668 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
670 for (x
= 0; x
< MAXSESSION
; x
++)
671 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
672 sprintf(s
, "%s%u ", s
, x
);
674 cli_print(cli
, "\tSessions:\t%s", s
);
680 // Show tunnel summary
681 cli_print(cli
, "%4s %20s %20s %6s %s",
688 for (i
= 1; i
< MAXTUNNEL
; i
++)
691 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
693 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
694 cli_print(cli
, "%4d %20s %20s %6s %6d %s",
696 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
697 fmtaddr(htonl(tunnel
[i
].ip
), 0),
698 states
[tunnel
[i
].state
],
701 ,(i
== TUNNEL_ID_PPPOE
)?"Tunnel pppoe":(tunnel
[i
].isremotelns
?"Tunnel To Remote LNS":"Tunnel To LAC")
703 ,(i
== TUNNEL_ID_PPPOE
)?"Tunnel pppoe":"Tunnel To LAC"
711 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
718 if (CLI_HELP_REQUESTED
)
719 return cli_arg_help(cli
, 1,
720 "USER", "Show details for specific username",
723 for (i
= 0; i
< MAXSESSION
; i
++)
725 if (!session
[i
].opened
) continue;
726 if (!session
[i
].user
[0]) continue;
730 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
732 if (strcmp(argv
[j
], session
[i
].user
) == 0)
734 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
735 sargv
[sargc
] = sid
[sargc
];
743 cli_print(cli
, "%s", session
[i
].user
);
747 return cmd_show_session(cli
, "users", sargv
, sargc
);
753 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
755 if (CLI_HELP_REQUESTED
)
756 return CLI_HELP_NO_ARGS
;
758 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
759 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
760 GET_STAT(tun_rx_bytes
),
761 GET_STAT(tun_rx_packets
),
762 GET_STAT(tun_rx_errors
),
763 GET_STAT(tun_rx_dropped
));
764 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
765 GET_STAT(tun_tx_bytes
),
766 GET_STAT(tun_tx_packets
),
767 GET_STAT(tun_tx_errors
));
770 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
771 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
772 GET_STAT(tunnel_rx_bytes
),
773 GET_STAT(tunnel_rx_packets
),
774 GET_STAT(tunnel_rx_errors
));
775 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
776 GET_STAT(tunnel_tx_bytes
),
777 GET_STAT(tunnel_tx_packets
),
778 GET_STAT(tunnel_tx_errors
),
779 GET_STAT(tunnel_retries
));
782 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
783 cli_print(cli
, "-----------------------------------------");
784 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
785 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
786 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
787 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
788 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
789 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
790 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
791 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
792 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
793 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
794 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
795 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
796 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
797 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
798 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
799 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
800 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
801 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
805 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
806 cli_print(cli
, "-----------------------------------------");
807 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
808 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
809 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
810 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
811 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
812 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
813 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
814 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
815 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
816 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
817 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
818 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
819 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
820 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
821 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
822 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
823 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
824 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
825 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
826 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
827 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
828 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
829 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
830 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
831 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
832 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
833 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
834 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
835 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
836 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
837 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
838 #endif /* STAT_CALLS */
841 time_t l
= GET_STAT(last_reset
);
843 char *p
= strchr(t
, '\n');
847 cli_print(cli
, "Last counter reset %s", t
);
853 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
855 if (CLI_HELP_REQUESTED
)
856 return CLI_HELP_NO_ARGS
;
858 memset(_statistics
, 0, sizeof(struct Tstats
));
859 SET_STAT(last_reset
, time(NULL
));
861 cli_print(cli
, "Counters cleared");
864 #endif /* STATISTICS */
866 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
868 if (CLI_HELP_REQUESTED
)
869 return CLI_HELP_NO_ARGS
;
871 cli_print(cli
, "L2TPNS %s", VERSION
);
875 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
878 int used
= 0, free
= 0, show_all
= 0;
880 if (!config
->cluster_iam_master
)
882 cli_print(cli
, "Can't do this on a slave. Do it on %s",
883 fmtaddr(config
->cluster_master_address
, 0));
888 if (CLI_HELP_REQUESTED
)
891 return cli_arg_help(cli
, 1, NULL
);
893 return cli_arg_help(cli
, 1,
894 "all", "Show all pool addresses, including unused",
898 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
902 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
903 for (i
= 0; i
< MAXIPPOOL
; i
++)
905 if (!ip_address_pool
[i
].address
) continue;
906 if (ip_address_pool
[i
].assigned
)
908 cli_print(cli
, "%-15s\tY %8d %s",
909 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
910 ip_address_pool
[i
].session
,
911 session
[ip_address_pool
[i
].session
].user
);
917 if (ip_address_pool
[i
].last
)
918 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
919 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
920 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
923 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
930 cli_print(cli
, "(Not displaying unused addresses)");
932 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
936 static FILE *save_config_fh
= 0;
937 static void print_save_config(struct cli_def
*cli
, char *string
)
940 fprintf(save_config_fh
, "%s\n", string
);
943 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
945 if (CLI_HELP_REQUESTED
)
946 return CLI_HELP_NO_ARGS
;
948 if ((save_config_fh
= fopen(config
->config_file
, "w")))
950 cli_print(cli
, "Writing configuration");
951 cli_print_callback(cli
, print_save_config
);
952 cmd_show_run(cli
, command
, argv
, argc
);
953 cli_print_callback(cli
, NULL
);
954 fclose(save_config_fh
);
959 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
964 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
966 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
969 char ipv6addr
[INET6_ADDRSTRLEN
];
971 if (CLI_HELP_REQUESTED
)
972 return CLI_HELP_NO_ARGS
;
974 cli_print(cli
, "# Current configuration:");
976 for (i
= 0; config_values
[i
].key
; i
++)
978 void *value
= ((void *)config
) + config_values
[i
].offset
;
979 if (config_values
[i
].type
== STRING
)
980 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
981 else if (config_values
[i
].type
== IPv4
)
982 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
983 else if (config_values
[i
].type
== IPv6
)
984 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
985 else if (config_values
[i
].type
== SHORT
)
986 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
987 else if (config_values
[i
].type
== BOOL
)
988 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
989 else if (config_values
[i
].type
== INT
)
990 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
991 else if (config_values
[i
].type
== UNSIGNED_LONG
)
992 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
995 cli_print(cli
, "# Plugins");
996 for (i
= 0; i
< MAXPLUGINS
; i
++)
998 if (*config
->plugins
[i
])
1000 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
1005 if (config
->as_number
)
1010 cli_print(cli
, "# BGP");
1011 cli_print(cli
, "router bgp %u", config
->as_number
);
1012 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1014 if (!config
->neighbour
[i
].name
[0])
1017 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
1019 k
= config
->neighbour
[i
].keepalive
;
1020 h
= config
->neighbour
[i
].hold
;
1027 k
= BGP_KEEPALIVE_TIME
;
1033 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1035 if (config
->neighbour
[i
].update_source
.s_addr
!= INADDR_ANY
)
1036 cli_print(cli
, " neighbour %s update-source %s",
1037 config
->neighbour
[i
].name
,
1038 inet_ntoa(config
->neighbour
[i
].update_source
));
1043 cli_print(cli
, "# Filters");
1044 for (i
= 0; i
< MAXFILTER
; i
++)
1046 ip_filter_rulet
*rules
;
1047 if (!*ip_filters
[i
].name
)
1050 cli_print(cli
, "ip access-list %s %s",
1051 ip_filters
[i
].extended
? "extended" : "standard",
1052 ip_filters
[i
].name
);
1054 rules
= ip_filters
[i
].rules
;
1055 while (rules
->action
)
1056 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1059 cli_print(cli
, "# end");
1063 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1065 int i
, free
= 0, used
= 0, show_all
= 0;
1077 if (CLI_HELP_REQUESTED
)
1080 return cli_arg_help(cli
, 1, NULL
);
1082 return cli_arg_help(cli
, 1,
1083 "all", "Show all RADIUS sessions, including unused",
1087 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1091 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1094 for (i
= 1; i
< MAXRADIUS
; i
++)
1096 if (radius
[i
].state
== RADIUSNULL
)
1101 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1103 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1107 states
[radius
[i
].state
],
1113 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1118 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1122 if (CLI_HELP_REQUESTED
)
1123 return CLI_HELP_NO_ARGS
;
1125 cli_print(cli
, "Plugins currently loaded:");
1126 for (i
= 0; i
< MAXPLUGINS
; i
++)
1127 if (*config
->plugins
[i
])
1128 cli_print(cli
, " %s", config
->plugins
[i
]);
1133 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1137 if (CLI_HELP_REQUESTED
)
1138 return CLI_HELP_NO_ARGS
;
1140 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1149 for (i
= 0; i
< MAXSESSION
; i
++)
1151 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1152 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1156 session
[i
].throttle_in
,
1157 session
[i
].throttle_out
,
1159 session
[i
].tbf_out
);
1165 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1167 if (CLI_HELP_REQUESTED
)
1168 return CLI_HELP_NO_ARGS
;
1170 cli_print(cli
, " _\n"
1176 " ( \\ `. `-. _,.-:\\\n"
1177 " \\ \\ `. `-._ __..--' ,-';/\n"
1178 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1179 " `. `. `-._ __..--' ,' /\n"
1180 " `. `-_ ``--..'' _.-' ,'\n"
1181 " `-_ `-.___ __,--' ,'\n"
1182 " `-.__ `----\"\"\" __.-'\n"
1183 "hh `--..____..--'");
1188 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1193 if (CLI_HELP_REQUESTED
)
1194 return cli_arg_help(cli
, argc
> 1,
1195 "USER", "Username of session to drop", NULL
);
1197 if (!config
->cluster_iam_master
)
1199 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1200 fmtaddr(config
->cluster_master_address
, 0));
1207 cli_error(cli
, "Specify a user to drop");
1211 for (i
= 0; i
< argc
; i
++)
1213 if (!(s
= sessionbyuser(argv
[i
])))
1215 cli_error(cli
, "User %s is not connected", argv
[i
]);
1219 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1221 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1222 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1229 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1234 if (CLI_HELP_REQUESTED
)
1235 return cli_arg_help(cli
, argc
> 1,
1236 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1238 if (!config
->cluster_iam_master
)
1240 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1241 fmtaddr(config
->cluster_master_address
, 0));
1248 cli_error(cli
, "Specify a tunnel to drop");
1252 for (i
= 0; i
< argc
; i
++)
1254 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1256 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1262 cli_error(cli
, "Tunnel %d is not connected", t
);
1268 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1272 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1273 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1279 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1284 if (CLI_HELP_REQUESTED
)
1285 return cli_arg_help(cli
, argc
> 1,
1286 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1288 if (!config
->cluster_iam_master
)
1290 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1291 fmtaddr(config
->cluster_master_address
, 0));
1298 cli_error(cli
, "Specify a session id to drop");
1302 for (i
= 0; i
< argc
; i
++)
1304 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1306 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1310 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1312 cli_print(cli
, "Dropping session %d", s
);
1313 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1316 else if (session
[s
].forwardtosession
&& session
[s
].opened
&& !session
[s
].die
)
1318 cli_print(cli
, "Dropping session %d", s
);
1319 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1324 cli_error(cli
, "Session %d is not active.", s
);
1331 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1337 if (CLI_HELP_REQUESTED
)
1342 return cli_arg_help(cli
, 0,
1343 "USER", "Username of session to snoop", NULL
);
1346 return cli_arg_help(cli
, 0,
1347 "A.B.C.D", "IP address of snoop destination", NULL
);
1350 return cli_arg_help(cli
, 0,
1351 "N", "Port of snoop destination", NULL
);
1355 return cli_arg_help(cli
, 1, NULL
);
1362 if (!config
->cluster_iam_master
)
1364 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1365 fmtaddr(config
->cluster_master_address
, 0));
1372 cli_error(cli
, "Specify username, ip and port");
1376 if (!(s
= sessionbyuser(argv
[0])))
1378 cli_error(cli
, "User %s is not connected", argv
[0]);
1382 ip
= inet_addr(argv
[1]);
1383 if (!ip
|| ip
== INADDR_NONE
)
1385 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1389 port
= atoi(argv
[2]);
1392 cli_error(cli
, "Invalid port %s", argv
[2]);
1396 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1397 cli_session_actions
[s
].snoop_ip
= ip
;
1398 cli_session_actions
[s
].snoop_port
= port
;
1399 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1404 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1409 if (CLI_HELP_REQUESTED
)
1410 return cli_arg_help(cli
, argc
> 1,
1411 "USER", "Username of session to unsnoop", NULL
);
1413 if (!config
->cluster_iam_master
)
1415 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1416 fmtaddr(config
->cluster_master_address
, 0));
1423 cli_error(cli
, "Specify a user to unsnoop");
1427 for (i
= 0; i
< argc
; i
++)
1429 if (!(s
= sessionbyuser(argv
[i
])))
1431 cli_error(cli
, "User %s is not connected", argv
[i
]);
1435 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1436 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1442 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1449 throttle USER - throttle in/out to default rate
1450 throttle USER RATE - throttle in/out to default rate
1451 throttle USER in RATE - throttle input only
1452 throttle USER out RATE - throttle output only
1453 throttle USER in RATE out RATE - throttle both
1456 if (CLI_HELP_REQUESTED
)
1461 return cli_arg_help(cli
, 0,
1462 "USER", "Username of session to throttle", NULL
);
1465 return cli_arg_help(cli
, 1,
1466 "RATE", "Rate in kbps (in and out)",
1467 "in", "Select incoming rate",
1468 "out", "Select outgoing rate", NULL
);
1471 return cli_arg_help(cli
, 1,
1472 "in", "Select incoming rate",
1473 "out", "Select outgoing rate", NULL
);
1476 if (isdigit(argv
[1][0]))
1477 return cli_arg_help(cli
, 1, NULL
);
1480 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1483 return cli_arg_help(cli
, argc
> 1, NULL
);
1487 if (!config
->cluster_iam_master
)
1489 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1490 fmtaddr(config
->cluster_master_address
, 0));
1497 cli_error(cli
, "Specify a user to throttle");
1501 if (!(s
= sessionbyuser(argv
[0])))
1503 cli_error(cli
, "User %s is not connected", argv
[0]);
1509 rate_in
= rate_out
= config
->rl_rate
;
1513 rate_in
= rate_out
= atoi(argv
[1]);
1516 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1520 else if (argc
== 3 || argc
== 5)
1523 for (i
= 1; i
< argc
- 1; i
+= 2)
1526 if (MATCH("in", argv
[i
]))
1527 r
= rate_in
= atoi(argv
[i
+1]);
1528 else if (MATCH("out", argv
[i
]))
1529 r
= rate_out
= atoi(argv
[i
+1]);
1533 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1540 cli_error(cli
, "Invalid arguments");
1544 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1546 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1550 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1551 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1552 cli_session_actions
[s
].throttle_in
= rate_in
;
1554 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1555 cli_session_actions
[s
].throttle_out
= rate_out
;
1557 if (cli_session_actions
[s
].throttle_in
== -1 &&
1558 cli_session_actions
[s
].throttle_out
== -1)
1560 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1564 cli_print(cli
, "Throttling user %s", argv
[0]);
1565 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1570 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1575 if (CLI_HELP_REQUESTED
)
1576 return cli_arg_help(cli
, argc
> 1,
1577 "USER", "Username of session to unthrottle", NULL
);
1579 if (!config
->cluster_iam_master
)
1581 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1582 fmtaddr(config
->cluster_master_address
, 0));
1589 cli_error(cli
, "Specify a user to unthrottle");
1593 for (i
= 0; i
< argc
; i
++)
1595 if (!(s
= sessionbyuser(argv
[i
])))
1597 cli_error(cli
, "User %s is not connected", argv
[i
]);
1601 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1603 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1604 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1608 cli_error(cli
, "User %s not throttled", argv
[i
]);
1615 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1619 if (CLI_HELP_REQUESTED
)
1620 return cli_arg_help(cli
, 1,
1621 "all", "Enable debugging for all except \"data\"",
1622 "critical", "", // FIXME: add descriptions
1632 char *p
= (char *) &debug_flags
;
1633 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1637 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1638 (debug_flags
.critical
) ? " critical" : "",
1639 (debug_flags
.error
) ? " error" : "",
1640 (debug_flags
.warning
) ? " warning" : "",
1641 (debug_flags
.info
) ? " info" : "",
1642 (debug_flags
.calls
) ? " calls" : "",
1643 (debug_flags
.data
) ? " data" : "");
1649 cli_print(cli
, "Debugging off");
1653 for (i
= 0; i
< argc
; i
++)
1655 int len
= strlen(argv
[i
]);
1657 if (argv
[i
][0] == 'c' && len
< 2)
1658 len
= 2; /* distinguish [cr]itical from [ca]lls */
1660 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1661 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1662 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1663 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1664 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1665 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1666 if (!strncmp("all", argv
[i
], len
))
1668 memset(&debug_flags
, 1, sizeof(debug_flags
));
1669 debug_flags
.data
= 0;
1673 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1679 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1683 if (CLI_HELP_REQUESTED
)
1684 return cli_arg_help(cli
, 1,
1685 "all", "Disable all debugging",
1686 "critical", "", // FIXME: add descriptions
1696 memset(&debug_flags
, 0, sizeof(debug_flags
));
1700 for (i
= 0; i
< argc
; i
++)
1702 int len
= strlen(argv
[i
]);
1704 if (argv
[i
][0] == 'c' && len
< 2)
1705 len
= 2; /* distinguish [cr]itical from [ca]lls */
1707 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1708 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1709 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1710 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1711 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1712 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1713 if (!strncmp("all", argv
[i
], len
))
1715 memset(&debug_flags
, 0, sizeof(debug_flags
));
1719 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1725 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1727 int i
, firstfree
= 0;
1729 if (CLI_HELP_REQUESTED
)
1730 return cli_arg_help(cli
, argc
> 1,
1731 "PLUGIN", "Name of plugin to load", NULL
);
1735 cli_error(cli
, "Specify a plugin to load");
1739 for (i
= 0; i
< MAXPLUGINS
; i
++)
1741 if (!*config
->plugins
[i
] && !firstfree
)
1743 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1745 cli_error(cli
, "Plugin is already loaded");
1752 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1753 config
->reload_config
= 1;
1754 cli_print(cli
, "Loading plugin %s", argv
[0]);
1760 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1764 if (CLI_HELP_REQUESTED
)
1765 return cli_arg_help(cli
, argc
> 1,
1766 "PLUGIN", "Name of plugin to unload", NULL
);
1770 cli_error(cli
, "Specify a plugin to remove");
1774 for (i
= 0; i
< MAXPLUGINS
; i
++)
1776 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1778 config
->reload_config
= 1;
1779 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1784 cli_error(cli
, "Plugin is not loaded");
1788 static char *duration(time_t secs
)
1790 static char *buf
= NULL
;
1793 if (!buf
) buf
= calloc(64, 1);
1797 int days
= secs
/ 86400;
1798 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1804 int mins
= secs
/ 60;
1805 int hrs
= mins
/ 60;
1808 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1810 else if (secs
>= 60)
1812 int mins
= secs
/ 60;
1813 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1816 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1821 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1824 char buf
[100], *p
= buf
, *loads
[3];
1825 int i
, num_sessions
= 0;
1827 if (CLI_HELP_REQUESTED
)
1828 return CLI_HELP_NO_ARGS
;
1830 fh
= fopen("/proc/loadavg", "r");
1831 fgets(buf
, 100, fh
);
1834 for (i
= 0; i
< 3; i
++)
1835 loads
[i
] = strdup(strsep(&p
, " "));
1838 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1840 for (i
= 1; i
< MAXSESSION
; i
++)
1841 if (session
[i
].opened
) num_sessions
++;
1843 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1845 duration(time_now
- config
->start_time
),
1847 loads
[0], loads
[1], loads
[2]
1849 for (i
= 0; i
< 3; i
++)
1850 if (loads
[i
]) free(loads
[i
]);
1852 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1857 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1861 if (CLI_HELP_REQUESTED
)
1867 int len
= strlen(argv
[0])-1;
1868 for (i
= 0; config_values
[i
].key
; i
++)
1869 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1870 cli_error(cli
, " %s", config_values
[i
].key
);
1876 return cli_arg_help(cli
, 0,
1877 "VALUE", "Value for variable", NULL
);
1881 return cli_arg_help(cli
, 1, NULL
);
1890 cli_error(cli
, "Specify variable and value");
1894 for (i
= 0; config_values
[i
].key
; i
++)
1896 void *value
= ((void *) config
) + config_values
[i
].offset
;
1897 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1899 // Found a value to set
1900 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1901 switch (config_values
[i
].type
)
1904 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1907 *(int *) value
= atoi(argv
[1]);
1910 *(unsigned long *) value
= atol(argv
[1]);
1913 *(short *) value
= atoi(argv
[1]);
1916 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1919 inet_pton(AF_INET6
, argv
[1], value
);
1922 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1928 cli_error(cli
, "Unknown variable type");
1931 config
->reload_config
= 1;
1936 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1940 int regular_stuff(struct cli_def
*cli
)
1946 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1948 char *m
= ringbuffer
->buffer
[i
].message
;
1954 switch (ringbuffer
->buffer
[i
].level
)
1956 case 0: show
= debug_flags
.critical
; break;
1957 case 1: show
= debug_flags
.error
; break;
1958 case 2: show
= debug_flags
.warning
; break;
1959 case 3: show
= debug_flags
.info
; break;
1960 case 4: show
= debug_flags
.calls
; break;
1961 case 5: show
= debug_flags
.data
; break;
1964 if (!show
) continue;
1966 if (!(p
= strchr(m
, '\n')))
1969 cli_error(cli
, "\r%s-%u-%u %.*s",
1970 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1971 ringbuffer
->buffer
[i
].tunnel
,
1972 ringbuffer
->buffer
[i
].session
,
1978 debug_rb_tail
= ringbuffer
->tail
;
1986 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1990 if (CLI_HELP_REQUESTED
)
1991 return cli_arg_help(cli
, argc
> 1,
1992 "<1-65535>", "Autonomous system number", NULL
);
1994 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1996 cli_error(cli
, "Invalid autonomous system number");
2000 if (bgp_configured
&& as
!= config
->as_number
)
2002 cli_error(cli
, "Can't change local AS on a running system");
2006 config
->as_number
= as
;
2007 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
2012 static int find_bgp_neighbour(char const *name
)
2017 in_addr_t addrs
[4] = { 0 };
2020 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
2023 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
2024 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
2026 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2028 if (!config
->neighbour
[i
].name
[0])
2030 if (new == -1) new = i
;
2034 if (!strcmp(name
, config
->neighbour
[i
].name
))
2037 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
2040 for (a
= h
->h_addr_list
; *a
; a
++)
2043 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
2044 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2052 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2058 if (CLI_HELP_REQUESTED
)
2063 return cli_arg_help(cli
, 0,
2064 "A.B.C.D", "BGP neighbour address",
2065 "NAME", "BGP neighbour name",
2069 return cli_arg_help(cli
, 0,
2070 "remote-as", "Set remote autonomous system number",
2071 "timers", "Set timers",
2072 "update-source", "Set source address to use for the BGP session",
2076 if (MATCH("remote-as", argv
[1]))
2077 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2079 if (MATCH("timers", argv
[1]))
2082 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2085 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2087 if (argc
== 5 && !argv
[4][1])
2088 return cli_arg_help(cli
, 1, NULL
);
2091 if (MATCH("update-source", argv
[1]))
2092 return cli_arg_help(cli
, argc
> 3, "A.B.C.D", "Source IP address", NULL
);
2100 cli_error(cli
, "Invalid arguments");
2104 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2106 cli_error(cli
, "Invalid neighbour");
2112 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2116 if (MATCH("remote-as", argv
[1]))
2118 int as
= atoi(argv
[2]);
2119 if (as
< 0 || as
> 65535)
2121 cli_error(cli
, "Invalid autonomous system number");
2125 if (!config
->neighbour
[i
].name
[0])
2127 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2128 config
->neighbour
[i
].keepalive
= -1;
2129 config
->neighbour
[i
].hold
= -1;
2130 config
->neighbour
[i
].update_source
.s_addr
= INADDR_ANY
;
2133 config
->neighbour
[i
].as
= as
;
2137 if (MATCH("update-source", argv
[1]))
2139 struct in_addr addr
;
2141 if (!config
->neighbour
[i
].name
[0])
2143 cli_error(cli
, "Specify remote-as first");
2147 if (!inet_aton(argv
[2], &addr
))
2149 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[2]);
2153 config
->neighbour
[i
].update_source
= addr
;
2157 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2159 cli_error(cli
, "Invalid arguments");
2163 if (!config
->neighbour
[i
].name
[0])
2165 cli_error(cli
, "Specify remote-as first");
2169 keepalive
= atoi(argv
[2]);
2170 hold
= atoi(argv
[3]);
2172 if (keepalive
< 1 || keepalive
> 65535)
2174 cli_error(cli
, "Invalid keepalive time");
2178 if (hold
< 3 || hold
> 65535)
2180 cli_error(cli
, "Invalid hold time");
2184 if (keepalive
== BGP_KEEPALIVE_TIME
)
2185 keepalive
= -1; // using default value
2187 if (hold
== BGP_HOLD_TIME
)
2190 config
->neighbour
[i
].keepalive
= keepalive
;
2191 config
->neighbour
[i
].hold
= hold
;
2196 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2200 if (CLI_HELP_REQUESTED
)
2201 return cli_arg_help(cli
, argc
> 0,
2202 "A.B.C.D", "BGP neighbour address",
2203 "NAME", "BGP neighbour name",
2208 cli_error(cli
, "Specify a BGP neighbour");
2212 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2214 cli_error(cli
, "Invalid neighbour");
2218 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2220 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2224 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2228 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2234 if (!bgp_configured
)
2237 if (CLI_HELP_REQUESTED
)
2238 return cli_arg_help(cli
, 1,
2239 "A.B.C.D", "BGP neighbour address",
2240 "NAME", "BGP neighbour name",
2243 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2244 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2248 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2250 if (!*bgp_peers
[i
].name
)
2253 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2254 if (argc
&& strcmp(addr
, argv
[0]) &&
2255 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2261 cli_print(cli
, "Peer AS Address "
2262 "State Retries Retry in Route Pend Timers");
2263 cli_print(cli
, "------------------ ----- --------------- "
2264 "----------- ------- -------- ----- ---- ---------");
2267 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2271 bgp_state_str(bgp_peers
[i
].state
),
2272 bgp_peers
[i
].retry_count
,
2273 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2274 bgp_peers
[i
].routing
? "yes" : "no",
2275 bgp_peers
[i
].update_routes
? "yes" : "no",
2276 bgp_peers
[i
].keepalive
,
2283 static int cmd_suspend_bgp(struct cli_def
*cli
, 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]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2309 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2310 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2316 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2321 if (!bgp_configured
)
2324 if (CLI_HELP_REQUESTED
)
2325 return cli_arg_help(cli
, 1,
2326 "A.B.C.D", "BGP neighbour address",
2327 "NAME", "BGP neighbour name",
2330 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2332 if (bgp_peers
[i
].state
!= Established
)
2335 if (bgp_peers
[i
].routing
)
2338 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2339 if (argc
&& strcmp(addr
, argv
[0]) &&
2340 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2343 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2344 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2350 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2355 if (!bgp_configured
)
2358 if (CLI_HELP_REQUESTED
)
2359 return cli_arg_help(cli
, 1,
2360 "A.B.C.D", "BGP neighbour address",
2361 "NAME", "BGP neighbour name",
2364 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2366 if (!*bgp_peers
[i
].name
)
2369 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2370 if (argc
&& strcmp(addr
, argv
[0]) &&
2371 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2374 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2375 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2383 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2387 if (CLI_HELP_REQUESTED
)
2392 return cli_arg_help(cli
, 0,
2393 "standard", "Standard syntax",
2394 "extended", "Extended syntax",
2398 return cli_arg_help(cli
, argv
[1][1],
2399 "NAME", "Access-list name",
2403 if (argc
== 3 && !argv
[2][1])
2404 return cli_arg_help(cli
, 1, NULL
);
2412 cli_error(cli
, "Specify access-list type and name");
2416 if (MATCH("standard", argv
[0]))
2418 else if (MATCH("extended", argv
[0]))
2422 cli_error(cli
, "Invalid access-list type");
2426 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2427 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2429 cli_error(cli
, "Invalid access-list name");
2433 filt
= find_filter(argv
[1], strlen(argv
[1]));
2438 cli_error(cli
, "Too many access-lists");
2443 if (!*ip_filters
[filt
].name
)
2445 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2446 strcpy(ip_filters
[filt
].name
, argv
[1]);
2447 ip_filters
[filt
].extended
= extended
;
2449 else if (ip_filters
[filt
].extended
!= extended
)
2451 cli_error(cli
, "Access-list is %s",
2452 ip_filters
[filt
].extended
? "extended" : "standard");
2457 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2461 if (filt
< 0 || !*ip_filters
[filt
].name
)
2463 cli_error(cli
, "Access-list not defined");
2468 if (ip_filters
[filt
].used
)
2470 cli_error(cli
, "Access-list in use");
2474 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2478 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2480 return access_list(cli
, argv
, argc
, 1);
2483 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2485 return access_list(cli
, argv
, argc
, 0);
2488 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2490 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2491 return sprintf(buf
, " any");
2493 if (wild
== INADDR_ANY
)
2494 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2496 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2499 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2503 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2504 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2505 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2506 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2507 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2513 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2515 static char buf
[256];
2518 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2521 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2522 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2525 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2529 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2530 p
+= show_ports(p
, &rule
->src_ports
);
2532 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2533 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2534 p
+= show_ports(p
, &rule
->dst_ports
);
2536 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2538 switch (rule
->tcp_flag_op
)
2540 case FILTER_FLAG_OP_EST
:
2541 p
+= sprintf(p
, " established");
2544 case FILTER_FLAG_OP_ANY
:
2545 case FILTER_FLAG_OP_ALL
:
2546 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2547 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2548 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2549 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2550 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2551 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2552 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2553 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2554 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2555 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2556 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2557 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2558 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2564 p
+= sprintf(p
, " fragments");
2569 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2571 static ip_filter_rulet rule
;
2572 struct in_addr addr
;
2576 if (CLI_HELP_REQUESTED
)
2580 cli_arg_help(cli
, 0,
2581 "ip", "Match IP packets",
2582 "tcp", "Match TCP packets",
2583 "udp", "Match UDP packets",
2589 // *sigh*, too darned complex
2590 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2596 cli_error(cli
, "Specify rule details");
2600 memset(&rule
, 0, sizeof(rule
));
2601 rule
.action
= (command
[0] == 'p')
2602 ? FILTER_ACTION_PERMIT
2603 : FILTER_ACTION_DENY
;
2605 if (MATCH("ip", argv
[0]))
2606 rule
.proto
= IPPROTO_IP
;
2607 else if (MATCH("udp", argv
[0]))
2608 rule
.proto
= IPPROTO_UDP
;
2609 else if (MATCH("tcp", argv
[0]))
2610 rule
.proto
= IPPROTO_TCP
;
2613 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2617 for (a
= 1, i
= 0; i
< 2; i
++)
2621 ip_filter_portt
*port
;
2626 wild
= &rule
.src_wild
;
2627 port
= &rule
.src_ports
;
2632 wild
= &rule
.dst_wild
;
2633 port
= &rule
.dst_ports
;
2636 cli_error(cli
, "Specify destination");
2641 if (MATCH("any", argv
[a
]))
2644 *wild
= INADDR_BROADCAST
;
2647 else if (MATCH("host", argv
[a
]))
2651 cli_error(cli
, "Specify host ip address");
2655 if (!inet_aton(argv
[a
], &addr
))
2657 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2669 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2673 if (!inet_aton(argv
[a
], &addr
))
2675 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2681 if (!inet_aton(argv
[++a
], &addr
))
2683 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2687 *wild
= addr
.s_addr
;
2691 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2695 if (MATCH("eq", argv
[a
]))
2696 port
->op
= FILTER_PORT_OP_EQ
;
2697 else if (MATCH("neq", argv
[a
]))
2698 port
->op
= FILTER_PORT_OP_NEQ
;
2699 else if (MATCH("gt", argv
[a
]))
2700 port
->op
= FILTER_PORT_OP_GT
;
2701 else if (MATCH("lt", argv
[a
]))
2702 port
->op
= FILTER_PORT_OP_LT
;
2703 else if (MATCH("range", argv
[a
]))
2704 port
->op
= FILTER_PORT_OP_RANGE
;
2711 cli_error(cli
, "Specify port");
2715 if (!(port
->port
= atoi(argv
[a
])))
2717 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2722 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2727 cli_error(cli
, "Specify port");
2731 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2733 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2740 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2742 if (MATCH("established", argv
[a
]))
2744 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2747 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2748 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2750 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2751 ? FILTER_FLAG_OP_ANY
2752 : FILTER_FLAG_OP_ALL
;
2756 cli_error(cli
, "Specify tcp flags");
2760 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2764 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2766 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2767 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2768 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2769 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2770 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2771 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2774 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2783 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2785 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2787 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2797 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2804 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2806 static ip_filter_rulet rule
;
2807 struct in_addr addr
;
2809 if (CLI_HELP_REQUESTED
)
2813 cli_arg_help(cli
, argv
[0][1],
2814 "A.B.C.D", "Source address",
2815 "any", "Any source address",
2816 "host", "Source host",
2822 if (MATCH("any", argv
[0]))
2824 if (argc
== 2 && !argv
[1][1])
2825 cli_arg_help(cli
, 1, NULL
);
2827 else if (MATCH("host", argv
[0]))
2831 cli_arg_help(cli
, argv
[1][1],
2832 "A.B.C.D", "Host address",
2835 else if (argc
== 3 && !argv
[2][1])
2836 cli_arg_help(cli
, 1, NULL
);
2842 cli_arg_help(cli
, 1,
2843 "A.B.C.D", "Wildcard bits",
2846 else if (argc
== 3 && !argv
[2][1])
2847 cli_arg_help(cli
, 1, NULL
);
2855 cli_error(cli
, "Specify rule details");
2859 memset(&rule
, 0, sizeof(rule
));
2860 rule
.action
= (command
[0] == 'p')
2861 ? FILTER_ACTION_PERMIT
2862 : FILTER_ACTION_DENY
;
2864 rule
.proto
= IPPROTO_IP
;
2865 if (MATCH("any", argv
[0]))
2867 rule
.src_ip
= INADDR_ANY
;
2868 rule
.src_wild
= INADDR_BROADCAST
;
2870 else if (MATCH("host", argv
[0]))
2874 cli_error(cli
, "Specify host ip address");
2878 if (!inet_aton(argv
[1], &addr
))
2880 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2884 rule
.src_ip
= addr
.s_addr
;
2885 rule
.src_wild
= INADDR_ANY
;
2891 cli_error(cli
, "Specify source ip address and wildcard");
2895 if (!inet_aton(argv
[0], &addr
))
2897 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2901 rule
.src_ip
= addr
.s_addr
;
2905 if (!inet_aton(argv
[1], &addr
))
2907 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2911 rule
.src_wild
= addr
.s_addr
;
2914 rule
.src_wild
= INADDR_ANY
;
2920 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2923 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2924 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2925 : access_list_rule_std(cli
, command
, argv
, argc
);
2930 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2932 if (!ip_filters
[filt
].rules
[i
].action
)
2934 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2938 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2942 cli_error(cli
, "Too many rules");
2946 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2951 /* filter USER {in|out} FILTER ... */
2952 if (CLI_HELP_REQUESTED
)
2957 return cli_arg_help(cli
, 0,
2958 "USER", "Username of session to filter", NULL
);
2962 return cli_arg_help(cli
, 0,
2963 "in", "Set incoming filter",
2964 "out", "Set outgoing filter", NULL
);
2968 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2969 "NAME", "Filter name", NULL
);
2972 return cli_arg_help(cli
, argc
> 1, NULL
);
2976 if (!config
->cluster_iam_master
)
2978 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2979 fmtaddr(config
->cluster_master_address
, 0));
2984 if (argc
!= 3 && argc
!= 5)
2986 cli_error(cli
, "Specify a user and filters");
2990 if (!(s
= sessionbyuser(argv
[0])))
2992 cli_error(cli
, "User %s is not connected", argv
[0]);
2996 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2997 for (i
= 1; i
< argc
; i
+= 2)
3002 if (MATCH("in", argv
[i
]))
3004 if (session
[s
].filter_in
)
3006 cli_error(cli
, "Input already filtered");
3009 f
= &cli_session_actions
[s
].filter_in
;
3011 else if (MATCH("out", argv
[i
]))
3013 if (session
[s
].filter_out
)
3015 cli_error(cli
, "Output already filtered");
3018 f
= &cli_session_actions
[s
].filter_out
;
3022 cli_error(cli
, "Invalid filter specification");
3026 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
3027 if (v
< 0 || !*ip_filters
[v
].name
)
3029 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
3036 cli_print(cli
, "Filtering user %s", argv
[0]);
3037 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
3042 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3047 if (CLI_HELP_REQUESTED
)
3048 return cli_arg_help(cli
, argc
> 1,
3049 "USER", "Username of session to remove filters from", NULL
);
3051 if (!config
->cluster_iam_master
)
3053 cli_error(cli
, "Can't do this on a slave. Do it on %s",
3054 fmtaddr(config
->cluster_master_address
, 0));
3061 cli_error(cli
, "Specify a user to remove filters from");
3065 for (i
= 0; i
< argc
; i
++)
3067 if (!(s
= sessionbyuser(argv
[i
])))
3069 cli_error(cli
, "User %s is not connected", argv
[i
]);
3073 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3075 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3076 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3080 cli_error(cli
, "User %s not filtered", argv
[i
]);
3087 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3091 if (CLI_HELP_REQUESTED
)
3092 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3096 cli_error(cli
, "Specify a filter name");
3100 for (i
= 0; i
< argc
; i
++)
3102 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
3103 ip_filter_rulet
*rules
;
3105 if (f
< 0 || !*ip_filters
[f
].name
)
3107 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3112 cli_print(cli
, " ");
3114 cli_print(cli
, "%s IP access list %s",
3115 ip_filters
[f
].extended
? "Extended" : "Standard",
3116 ip_filters
[f
].name
);
3118 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3120 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3122 cli_print(cli
, "%s (%u match%s)", r
,
3123 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3125 cli_print(cli
, "%s", r
);
3132 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3134 if (CLI_HELP_REQUESTED
)
3135 return CLI_HELP_NO_ARGS
;
3137 kill(getppid(), SIGQUIT
);
3141 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3143 if (CLI_HELP_REQUESTED
)
3144 return CLI_HELP_NO_ARGS
;
3146 kill(getppid(), SIGHUP
);
3150 static int cmd_show_group(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3155 if (CLI_HELP_REQUESTED
)
3156 return cli_arg_help(cli
, 1,
3157 "<1-%d>", MAXGROUPE
-1, "Show specific group by id",
3163 // Show individual group
3164 for (i
= 0; i
< argc
; i
++)
3169 if (g
<= 0 || g
>= MAXGROUPE
)
3171 cli_print(cli
, "Invalid group id \"%s\"", argv
[i
]);
3175 cli_print(cli
, "\r\nGroup %d:", g
);
3176 cli_print(cli
, "\tNb Session:\t\t%d", grpsession
[g
].nbsession
);
3177 cli_print(cli
, "\tNb Routes:\t\t%d", grpsession
[g
].nbroutesgrp
);
3178 cli_print(cli
, "\ttime_changed:\t\t%d\n", grpsession
[g
].time_changed
);
3180 for (i
= 0; i
< grpsession
[g
].nbsession
; i
++)
3182 if ((s
= grpsession
[g
].sesslist
[i
].sid
))
3184 cli_print(cli
, "\tSession:\t%d\tTx Rate:%d Kbps\t\t\tweight:\t%d",
3186 grpsession
[g
].sesslist
[i
].tx_rate
/(1024/8),
3187 grpsession
[g
].sesslist
[i
].weight
);
3191 for (i
= 0; i
< grpsession
[g
].nbroutesgrp
; i
++)
3193 if (grpsession
[g
].route
[i
].ip
!= 0)
3195 cli_print(cli
, "\tRoute:\t%s/%d", fmtaddr(htonl(grpsession
[g
].route
[i
].ip
), 0), grpsession
[g
].route
[i
].prefixlen
);
3203 cli_print(cli
, "%5s %7s %9s %12s",
3209 for (g
= gnextgrpid
; g
!= 0; g
= grpsession
[g
].prev
)
3211 cli_print(cli
, "%5d %7d %9d %12d",
3213 grpsession
[g
].nbsession
,
3214 grpsession
[g
].nbroutesgrp
,
3215 grpsession
[g
].time_changed
);
3223 static int cmd_setforward(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3227 if (CLI_HELP_REQUESTED
)
3232 return cli_arg_help(cli
, 0,
3233 "MASK", "Users mask to forward (ex: myISP@operator.com)", NULL
);
3236 return cli_arg_help(cli
, 0,
3237 "IP", "IP of the remote LNS(ex: 64.64.64.64)", NULL
);
3240 return cli_arg_help(cli
, 0,
3241 "PORT", "Port of the remote LNS (ex: 1701)", NULL
);
3244 return cli_arg_help(cli
, 0,
3245 "SECRET", "l2tp secret of the remote LNS (ex: mysecretpsw)", NULL
);
3248 return cli_arg_help(cli
, argc
> 1, NULL
);
3254 cli_error(cli
, "Specify variable and value");
3258 // lac_addremotelns(mask, IP_RemoteLNS, Port_RemoteLNS, SecretRemoteLNS)
3259 ret
= lac_addremotelns(argv
[0], argv
[1], argv
[2], argv
[3]);
3263 cli_print(cli
, "setforward %s %s %s %s", argv
[0], argv
[1], argv
[2], argv
[3]);
3265 cli_print(cli
, "%s Updated, the tunnel must be dropped", argv
[0]);
3268 cli_error(cli
, "ERROR setforward %s %s %s %s", argv
[0], argv
[1], argv
[2], argv
[3]);
3273 static int cmd_show_rmtlnsconf(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3278 if (CLI_HELP_REQUESTED
)
3280 return cli_arg_help(cli
, 0, "remotelns-conf", "Show a list of remote LNS configurations", NULL
);
3283 for (idrlns
= 0; idrlns
< MAXRLNSTUNNEL
; idrlns
++)
3285 if (lac_cli_show_remotelns(idrlns
, strdisp
) != 0)
3286 cli_print(cli
, "%s", strdisp
);