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"
37 extern tunnelt
*tunnel
;
38 extern bundlet
*bundle
;
39 extern sessiont
*session
;
40 extern radiust
*radius
;
41 extern ippoolt
*ip_address_pool
;
42 extern struct Tstats
*_statistics
;
43 static struct cli_def
*cli
= NULL
;
44 extern configt
*config
;
45 extern config_descriptt config_values
[];
47 extern struct Tringbuffer
*ringbuffer
;
49 extern struct cli_session_actions
*cli_session_actions
;
50 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
51 extern tbft
*filter_list
;
52 extern ip_filtert
*ip_filters
;
66 static int debug_rb_tail
;
67 static char *debug_levels
[] = {
78 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 static int cmd_show_group(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
102 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
103 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
104 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
105 static int cmd_setforward(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
106 static int cmd_show_rmtlnsconf(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int regular_stuff(struct cli_def
*cli
);
111 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
112 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
113 #endif /* STATISTICS */
116 #define MODE_CONFIG_BGP 8
117 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
118 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
120 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
121 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
122 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
123 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
126 #define MODE_CONFIG_NACL 9
127 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
128 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
129 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
130 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
131 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
132 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
134 /* match if b is a substr of a */
135 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
141 struct cli_command
*c
;
142 struct cli_command
*c2
;
146 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
147 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
149 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
151 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
152 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
153 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
154 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
155 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
156 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
157 cli_register_command(cli
, c
, "remotelns-conf", cmd_show_rmtlnsconf
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Show a list of remote LNS configuration");
158 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
159 cli_register_command(cli
, c
, "group", cmd_show_group
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of groups or details for a single group");
160 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
161 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
162 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
163 cli_register_command(cli
, c
, "users", cmd_show_users
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of all connected users or details of selected user");
164 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
165 cli_register_command(cli
, c
, "access-list", cmd_show_access_list
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show named access-list");
167 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
168 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
169 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
172 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
174 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
175 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
178 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
179 cli_register_command(cli
, NULL
, "shutdown", cmd_shutdown
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Shutdown l2tpns daemon and exit");
180 cli_register_command(cli
, NULL
, "reload", cmd_reload
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Reload configuration");
182 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
183 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
184 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
186 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable interception of a session");
187 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Enable throttling of a session");
188 cli_register_command(cli
, NULL
, "filter", cmd_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Add filtering to a session");
189 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
192 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
193 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
196 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
197 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable interception of a session");
198 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disable throttling of a session");
199 cli_register_command(cli
, c
, "filter", cmd_no_filter
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Remove filtering from a session");
200 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
203 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
204 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
206 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
207 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
209 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
210 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
212 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
214 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
215 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
218 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
219 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
220 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
221 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
223 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
224 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
226 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
227 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
229 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
231 cli_register_command(cli
, NULL
, "setforward", cmd_setforward
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set the Remote LNS Forward");
233 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
234 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
236 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
237 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
239 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
240 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
241 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
243 // Enable regular processing
244 cli_regular(cli
, regular_stuff
);
246 if (!(f
= fopen(CLIUSERS
, "r")))
248 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
252 while (fgets(buf
, 4096, f
))
255 if (*buf
== '#') continue;
256 if ((p
= strchr(buf
, '\r'))) *p
= 0;
257 if ((p
= strchr(buf
, '\n'))) *p
= 0;
259 if (!(p
= strchr((char *)buf
, ':'))) continue;
261 if (!strcmp(buf
, "enable"))
263 cli_allow_enable(cli
, p
);
264 LOG(3, 0, 0, "Setting enable password\n");
268 cli_allow_user(cli
, buf
, p
);
269 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
276 void cli_init_complete(char *hostname
)
279 struct sockaddr_in addr
;
281 if (hostname
&& *hostname
)
282 cli_set_hostname(cli
, hostname
);
284 cli_set_hostname(cli
, "l2tpns");
286 memset(&addr
, 0, sizeof(addr
));
287 clifd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
288 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
291 // Set cli fd as non-blocking
292 flags
= fcntl(clifd
, F_GETFL
, 0);
293 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
295 addr
.sin_family
= AF_INET
;
296 addr
.sin_addr
.s_addr
= config
->cli_bind_address
; /* defaults to INADDR_ANY */
297 addr
.sin_port
= htons(23);
298 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
300 LOG(0, 0, 0, "Error binding cli on port 23: %s\n", strerror(errno
));
305 if (listen(clifd
, 10) < 0)
307 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
314 void cli_do(int sockfd
)
316 int require_auth
= 1;
317 struct sockaddr_in addr
;
318 socklen_t l
= sizeof(addr
);
320 if (fork_and_close()) return;
321 if (getpeername(sockfd
, (struct sockaddr
*) &addr
, &l
) == 0)
323 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
324 LOG(require_auth
? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
325 fmtaddr(addr
.sin_addr
.s_addr
, 0));
328 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
332 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
333 if (!cli
->users
) /* paranoia */
335 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
341 /* no username/pass required */
346 debug_rb_tail
= ringbuffer
->tail
;
348 memset(&debug_flags
, 0, sizeof(debug_flags
));
349 debug_flags
.critical
= 1;
351 cli_loop(cli
, sockfd
);
354 LOG(require_auth
? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
355 fmtaddr(addr
.sin_addr
.s_addr
, 0));
360 static void cli_print_log(struct cli_def
*cli
, char *string
)
362 LOG(3, 0, 0, "%s\n", string
);
365 void cli_do_file(FILE *fh
)
367 LOG(3, 0, 0, "Reading configuration file\n");
368 cli_print_callback(cli
, cli_print_log
);
369 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
370 cli_print_callback(cli
, NULL
);
373 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
384 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
386 int v
= va_arg(ap
, int);
387 snprintf(buf
, sizeof(buf
), entry
, v
);
393 desc
= va_arg(ap
, char *);
395 cli_error(cli
, " %-20s %s", p
, desc
);
397 cli_error(cli
, " %s", p
);
399 entry
= desc
? va_arg(ap
, char *) : 0;
404 cli_error(cli
, " <cr>");
409 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
413 if (CLI_HELP_REQUESTED
)
414 return cli_arg_help(cli
, 1,
415 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
421 // Show individual session
422 for (i
= 0; i
< argc
; i
++)
424 unsigned int s
, b_in
, b_out
;
426 if (s
<= 0 || s
>= MAXSESSION
)
428 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
431 cli_print(cli
, "\r\nSession %d:", s
);
432 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
433 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
434 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
435 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
436 cli_print(cli
, "\tPPP Phase:\t%s", ppp_phase(session
[s
].ppp
.phase
));
437 switch (session
[s
].ppp
.phase
)
440 cli_print(cli
, "\t LCP state:\t%s", ppp_state(session
[s
].ppp
.lcp
));
445 cli_print(cli
, "\t IPCP state:\t%s", ppp_state(session
[s
].ppp
.ipcp
));
446 cli_print(cli
, "\t IPV6CP state:\t%s", ppp_state(session
[s
].ppp
.ipv6cp
));
447 cli_print(cli
, "\t CCP state:\t%s", ppp_state(session
[s
].ppp
.ccp
));
449 cli_print(cli
, "\tIP address:\t%s", fmtaddr(htonl(session
[s
].ip
), 0));
450 cli_print(cli
, "\tUnique SID:\t%u", session
[s
].unique_id
);
451 cli_print(cli
, "\tOpened:\t\t%u seconds", session
[s
].opened
? abs(time_now
- session
[s
].opened
) : 0);
452 cli_print(cli
, "\tIdle time:\t%u seconds", session
[s
].last_packet
? abs(time_now
- session
[s
].last_packet
) : 0);
453 if (session
[s
].session_timeout
)
455 clockt opened
= session
[s
].opened
;
456 if (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
457 opened
= bundle
[session
[s
].bundle
].online_time
;
459 cli_print(cli
, "\tSess Timeout:\t%u seconds", session
[s
].session_timeout
- (opened
? abs(time_now
- opened
) : 0));
462 if (session
[s
].idle_timeout
)
463 cli_print(cli
, "\tIdle Timeout:\t%u seconds", session
[s
].idle_timeout
- (session
[s
].last_data
? abs(time_now
- session
[s
].last_data
) : 0));
465 if (session
[s
].timeout
)
467 cli_print(cli
, "\tRemaining time:\t%u",
468 (session
[s
].bundle
&& bundle
[session
[s
].bundle
].num_of_links
> 1)
469 ? (unsigned) (session
[s
].timeout
- bundle
[session
[s
].bundle
].online_time
)
470 : (unsigned) (session
[s
].timeout
- (time_now
- session
[s
].opened
)));
473 cli_print(cli
, "\tBytes In/Out:\t%u/%u", session
[s
].cout
, session
[s
].cin
);
474 cli_print(cli
, "\tPkts In/Out:\t%u/%u", session
[s
].pout
, session
[s
].pin
);
475 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
476 cli_print(cli
, "\tRx Speed:\t%u", session
[s
].rx_connect_speed
);
477 cli_print(cli
, "\tTx Speed:\t%u", session
[s
].tx_connect_speed
);
478 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
479 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
- 1].name
);
480 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
481 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
- 1].name
);
482 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
483 cli_print(cli
, "\tIntercepted:\t%s:%d", fmtaddr(session
[s
].snoop_ip
, 0), session
[s
] .snoop_port
);
485 cli_print(cli
, "\tIntercepted:\tno");
487 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
489 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
490 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
491 t
? "YES" : "no", t
? " (" : "",
492 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
494 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
498 b_in
= session
[s
].tbf_in
;
499 b_out
= session
[s
].tbf_out
;
501 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
502 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
503 "ByteSent", "PackSent", "PackDrop", "PackDelay");
506 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
508 (filter_list
[b_in
].next
? "*" : " "),
509 (b_in
< 100 ? "\t" : ""),
510 filter_list
[b_in
].rate
* 8,
511 filter_list
[b_in
].credit
,
512 filter_list
[b_in
].queued
,
513 filter_list
[b_in
].b_queued
,
514 filter_list
[b_in
].p_queued
,
515 filter_list
[b_in
].b_sent
,
516 filter_list
[b_in
].p_sent
,
517 filter_list
[b_in
].p_dropped
,
518 filter_list
[b_in
].p_delayed
);
521 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
523 (filter_list
[b_out
].next
? "*" : " "),
524 (b_out
< 100 ? "\t" : ""),
525 filter_list
[b_out
].rate
* 8,
526 filter_list
[b_out
].credit
,
527 filter_list
[b_out
].queued
,
528 filter_list
[b_out
].b_queued
,
529 filter_list
[b_out
].p_queued
,
530 filter_list
[b_out
].b_sent
,
531 filter_list
[b_out
].p_sent
,
532 filter_list
[b_out
].p_dropped
,
533 filter_list
[b_out
].p_delayed
);
540 cli_print(cli
, "%5s %7s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-18s %s",
555 "LAC(L)/RLNS(R)/PPPOE(P)",
558 for (i
= 1; i
< MAXSESSION
; i
++)
561 if (!session
[i
].opened
) continue;
562 if (session
[i
].bundle
&& bundle
[session
[i
].bundle
].num_of_links
> 1)
563 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- bundle
[session
[i
].bundle
].online_time
) : 0;
565 rem_time
= session
[i
].timeout
? (session
[i
].timeout
- (time_now
-session
[i
].opened
)) : 0;
566 cli_print(cli
, "%5d %7d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-20s %s",
568 session
[i
].forwardtosession
,
570 session
[i
].user
[0] ? session
[i
].user
: "*",
571 fmtaddr(htonl(session
[i
].ip
), 0),
572 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
573 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
574 (session
[i
].walled_garden
) ? "Y" : "N",
575 (session
[i
].ppp
.ipv6cp
== Opened
) ? "Y" : "N",
576 abs(time_now
- (unsigned long)session
[i
].opened
),
577 (unsigned long)session
[i
].cout
,
578 (unsigned long)session
[i
].cin
,
579 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
580 (unsigned long)(rem_time
),
581 (session
[i
].tunnel
== TUNNEL_ID_PPPOE
)?"(P)":(tunnel
[session
[i
].tunnel
].isremotelns
?"(R)":"(L)"),
582 (session
[i
].tunnel
== TUNNEL_ID_PPPOE
)?fmtMacAddr(session
[i
].src_hwaddr
):fmtaddr(htonl(tunnel
[session
[i
].tunnel
].ip
), 1),
583 session
[i
].calling
[0] ? session
[i
].calling
: "*");
588 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
590 int i
, x
, show_all
= 0;
598 if (CLI_HELP_REQUESTED
)
601 return cli_arg_help(cli
, 1,
602 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
605 return cli_arg_help(cli
, 1,
606 "all", "Show all tunnels, including unused",
607 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
614 if (strcmp(argv
[0], "all") == 0)
620 // Show individual tunnel
621 for (i
= 0; i
< argc
; i
++)
626 if (t
<= 0 || t
>= MAXTUNNEL
)
628 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
631 cli_print(cli
, "\r\nTunnel %d:", t
);
632 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
633 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
634 cli_print(cli
, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel
[t
].ip
), 0));
635 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
636 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
637 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
638 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
639 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
640 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
642 for (x
= 0; x
< MAXSESSION
; x
++)
643 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
644 sprintf(s
, "%s%u ", s
, x
);
646 cli_print(cli
, "\tSessions:\t%s", s
);
652 // Show tunnel summary
653 cli_print(cli
, "%4s %20s %20s %6s %s",
660 for (i
= 1; i
< MAXTUNNEL
; i
++)
663 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
665 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
666 cli_print(cli
, "%4d %20s %20s %6s %6d %s",
668 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
669 fmtaddr(htonl(tunnel
[i
].ip
), 0),
670 states
[tunnel
[i
].state
],
672 ,(i
== TUNNEL_ID_PPPOE
)?"Tunnel pppoe":(tunnel
[i
].isremotelns
?"Tunnel To Remote LNS":"Tunnel To LAC")
679 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
686 if (CLI_HELP_REQUESTED
)
687 return cli_arg_help(cli
, 1,
688 "USER", "Show details for specific username",
691 for (i
= 0; i
< MAXSESSION
; i
++)
693 if (!session
[i
].opened
) continue;
694 if (!session
[i
].user
[0]) continue;
698 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
700 if (strcmp(argv
[j
], session
[i
].user
) == 0)
702 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
703 sargv
[sargc
] = sid
[sargc
];
711 cli_print(cli
, "%s", session
[i
].user
);
715 return cmd_show_session(cli
, "users", sargv
, sargc
);
721 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
723 if (CLI_HELP_REQUESTED
)
724 return CLI_HELP_NO_ARGS
;
726 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
727 cli_print(cli
, "%-10s %10u %10u %10u %10u", "RX",
728 GET_STAT(tun_rx_bytes
),
729 GET_STAT(tun_rx_packets
),
730 GET_STAT(tun_rx_errors
),
731 GET_STAT(tun_rx_dropped
));
732 cli_print(cli
, "%-10s %10u %10u %10u", "TX",
733 GET_STAT(tun_tx_bytes
),
734 GET_STAT(tun_tx_packets
),
735 GET_STAT(tun_tx_errors
));
738 cli_print(cli
, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
739 cli_print(cli
, "%-10s %10u %10u %10u", "RX",
740 GET_STAT(tunnel_rx_bytes
),
741 GET_STAT(tunnel_rx_packets
),
742 GET_STAT(tunnel_rx_errors
));
743 cli_print(cli
, "%-10s %10u %10u %10u %10u", "TX",
744 GET_STAT(tunnel_tx_bytes
),
745 GET_STAT(tunnel_tx_packets
),
746 GET_STAT(tunnel_tx_errors
),
747 GET_STAT(tunnel_retries
));
750 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
751 cli_print(cli
, "-----------------------------------------");
752 cli_print(cli
, "%-30s%u", "radius_retries", GET_STAT(radius_retries
));
753 cli_print(cli
, "%-30s%u", "arp_sent", GET_STAT(arp_sent
));
754 cli_print(cli
, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped
));
755 cli_print(cli
, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created
));
756 cli_print(cli
, "%-30s%u", "session_created", GET_STAT(session_created
));
757 cli_print(cli
, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout
));
758 cli_print(cli
, "%-30s%u", "session_timeout", GET_STAT(session_timeout
));
759 cli_print(cli
, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout
));
760 cli_print(cli
, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow
));
761 cli_print(cli
, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow
));
762 cli_print(cli
, "%-30s%u", "session_overflow", GET_STAT(session_overflow
));
763 cli_print(cli
, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated
));
764 cli_print(cli
, "%-30s%u", "ip_freed", GET_STAT(ip_freed
));
765 cli_print(cli
, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded
));
766 cli_print(cli
, "%-30s%u", "recv_forward", GET_STAT(recv_forward
));
767 cli_print(cli
, "%-30s%u", "select_called", GET_STAT(select_called
));
768 cli_print(cli
, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used
));
769 cli_print(cli
, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded
));
773 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
774 cli_print(cli
, "-----------------------------------------");
775 cli_print(cli
, "%-30s%u", "call_processtun", GET_STAT(call_processtun
));
776 cli_print(cli
, "%-30s%u", "call_processipout", GET_STAT(call_processipout
));
777 cli_print(cli
, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out
));
778 cli_print(cli
, "%-30s%u", "call_processudp", GET_STAT(call_processudp
));
779 cli_print(cli
, "%-30s%u", "call_processpap", GET_STAT(call_processpap
));
780 cli_print(cli
, "%-30s%u", "call_processchap", GET_STAT(call_processchap
));
781 cli_print(cli
, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp
));
782 cli_print(cli
, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp
));
783 cli_print(cli
, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp
));
784 cli_print(cli
, "%-30s%u", "call_processipin", GET_STAT(call_processipin
));
785 cli_print(cli
, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in
));
786 cli_print(cli
, "%-30s%u", "call_processccp", GET_STAT(call_processccp
));
787 cli_print(cli
, "%-30s%u", "call_processrad", GET_STAT(call_processrad
));
788 cli_print(cli
, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp
));
789 cli_print(cli
, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp
));
790 cli_print(cli
, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap
));
791 cli_print(cli
, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip
));
792 cli_print(cli
, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6
));
793 cli_print(cli
, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
794 cli_print(cli
, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend
));
795 cli_print(cli
, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill
));
796 cli_print(cli
, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
797 cli_print(cli
, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill
));
798 cli_print(cli
, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
799 cli_print(cli
, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup
));
800 cli_print(cli
, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
801 cli_print(cli
, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address
));
802 cli_print(cli
, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
803 cli_print(cli
, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend
));
804 cli_print(cli
, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry
));
805 cli_print(cli
, "%-30s%u", "call_random_data", GET_STAT(call_random_data
));
806 #endif /* STAT_CALLS */
809 time_t l
= GET_STAT(last_reset
);
811 char *p
= strchr(t
, '\n');
815 cli_print(cli
, "Last counter reset %s", t
);
821 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
823 if (CLI_HELP_REQUESTED
)
824 return CLI_HELP_NO_ARGS
;
826 memset(_statistics
, 0, sizeof(struct Tstats
));
827 SET_STAT(last_reset
, time(NULL
));
829 cli_print(cli
, "Counters cleared");
832 #endif /* STATISTICS */
834 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
836 if (CLI_HELP_REQUESTED
)
837 return CLI_HELP_NO_ARGS
;
839 cli_print(cli
, "L2TPNS %s", VERSION
);
843 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
846 int used
= 0, free
= 0, show_all
= 0;
848 if (!config
->cluster_iam_master
)
850 cli_print(cli
, "Can't do this on a slave. Do it on %s",
851 fmtaddr(config
->cluster_master_address
, 0));
856 if (CLI_HELP_REQUESTED
)
859 return cli_arg_help(cli
, 1, NULL
);
861 return cli_arg_help(cli
, 1,
862 "all", "Show all pool addresses, including unused",
866 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
870 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
871 for (i
= 0; i
< MAXIPPOOL
; i
++)
873 if (!ip_address_pool
[i
].address
) continue;
874 if (ip_address_pool
[i
].assigned
)
876 cli_print(cli
, "%-15s\tY %8d %s",
877 fmtaddr(htonl(ip_address_pool
[i
].address
), 0),
878 ip_address_pool
[i
].session
,
879 session
[ip_address_pool
[i
].session
].user
);
885 if (ip_address_pool
[i
].last
)
886 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
887 fmtaddr(htonl(ip_address_pool
[i
].address
), 0), "",
888 ip_address_pool
[i
].user
, (int) time_now
- ip_address_pool
[i
].last
);
891 cli_print(cli
, "%-15s\tN", fmtaddr(htonl(ip_address_pool
[i
].address
), 0));
898 cli_print(cli
, "(Not displaying unused addresses)");
900 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
904 static FILE *save_config_fh
= 0;
905 static void print_save_config(struct cli_def
*cli
, char *string
)
908 fprintf(save_config_fh
, "%s\n", string
);
911 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
913 if (CLI_HELP_REQUESTED
)
914 return CLI_HELP_NO_ARGS
;
916 if ((save_config_fh
= fopen(config
->config_file
, "w")))
918 cli_print(cli
, "Writing configuration");
919 cli_print_callback(cli
, print_save_config
);
920 cmd_show_run(cli
, command
, argv
, argc
);
921 cli_print_callback(cli
, NULL
);
922 fclose(save_config_fh
);
927 cli_error(cli
, "Error writing configuration: %s", strerror(errno
));
932 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
934 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
937 char ipv6addr
[INET6_ADDRSTRLEN
];
939 if (CLI_HELP_REQUESTED
)
940 return CLI_HELP_NO_ARGS
;
942 cli_print(cli
, "# Current configuration:");
944 for (i
= 0; config_values
[i
].key
; i
++)
946 void *value
= ((void *)config
) + config_values
[i
].offset
;
947 if (config_values
[i
].type
== STRING
)
948 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *) value
);
949 else if (config_values
[i
].type
== IPv4
)
950 cli_print(cli
, "set %s %s", config_values
[i
].key
, fmtaddr(*(in_addr_t
*) value
, 0));
951 else if (config_values
[i
].type
== IPv6
)
952 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_ntop(AF_INET6
, value
, ipv6addr
, INET6_ADDRSTRLEN
));
953 else if (config_values
[i
].type
== SHORT
)
954 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *) value
);
955 else if (config_values
[i
].type
== BOOL
)
956 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *) value
) ? "yes" : "no");
957 else if (config_values
[i
].type
== INT
)
958 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *) value
);
959 else if (config_values
[i
].type
== UNSIGNED_LONG
)
960 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *) value
);
963 cli_print(cli
, "# Plugins");
964 for (i
= 0; i
< MAXPLUGINS
; i
++)
966 if (*config
->plugins
[i
])
968 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
973 if (config
->as_number
)
978 cli_print(cli
, "# BGP");
979 cli_print(cli
, "router bgp %u", config
->as_number
);
980 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
982 if (!config
->neighbour
[i
].name
[0])
985 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
987 k
= config
->neighbour
[i
].keepalive
;
988 h
= config
->neighbour
[i
].hold
;
995 k
= BGP_KEEPALIVE_TIME
;
1001 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
1003 if (config
->neighbour
[i
].update_source
.s_addr
!= INADDR_ANY
)
1004 cli_print(cli
, " neighbour %s update-source %s",
1005 config
->neighbour
[i
].name
,
1006 inet_ntoa(config
->neighbour
[i
].update_source
));
1011 cli_print(cli
, "# Filters");
1012 for (i
= 0; i
< MAXFILTER
; i
++)
1014 ip_filter_rulet
*rules
;
1015 if (!*ip_filters
[i
].name
)
1018 cli_print(cli
, "ip access-list %s %s",
1019 ip_filters
[i
].extended
? "extended" : "standard",
1020 ip_filters
[i
].name
);
1022 rules
= ip_filters
[i
].rules
;
1023 while (rules
->action
)
1024 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
1027 cli_print(cli
, "# end");
1031 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1033 int i
, free
= 0, used
= 0, show_all
= 0;
1045 if (CLI_HELP_REQUESTED
)
1048 return cli_arg_help(cli
, 1, NULL
);
1050 return cli_arg_help(cli
, 1,
1051 "all", "Show all RADIUS sessions, including unused",
1055 cli_print(cli
, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1059 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1062 for (i
= 1; i
< MAXRADIUS
; i
++)
1064 if (radius
[i
].state
== RADIUSNULL
)
1069 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1071 cli_print(cli
, "%6d%7d%5d%6s%9d%9u%4d",
1075 states
[radius
[i
].state
],
1081 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1086 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1090 if (CLI_HELP_REQUESTED
)
1091 return CLI_HELP_NO_ARGS
;
1093 cli_print(cli
, "Plugins currently loaded:");
1094 for (i
= 0; i
< MAXPLUGINS
; i
++)
1095 if (*config
->plugins
[i
])
1096 cli_print(cli
, " %s", config
->plugins
[i
]);
1101 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1105 if (CLI_HELP_REQUESTED
)
1106 return CLI_HELP_NO_ARGS
;
1108 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1117 for (i
= 0; i
< MAXSESSION
; i
++)
1119 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1120 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1124 session
[i
].throttle_in
,
1125 session
[i
].throttle_out
,
1127 session
[i
].tbf_out
);
1133 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1135 if (CLI_HELP_REQUESTED
)
1136 return CLI_HELP_NO_ARGS
;
1138 cli_print(cli
, " _\n"
1144 " ( \\ `. `-. _,.-:\\\n"
1145 " \\ \\ `. `-._ __..--' ,-';/\n"
1146 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1147 " `. `. `-._ __..--' ,' /\n"
1148 " `. `-_ ``--..'' _.-' ,'\n"
1149 " `-_ `-.___ __,--' ,'\n"
1150 " `-.__ `----\"\"\" __.-'\n"
1151 "hh `--..____..--'");
1156 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1161 if (CLI_HELP_REQUESTED
)
1162 return cli_arg_help(cli
, argc
> 1,
1163 "USER", "Username of session to drop", NULL
);
1165 if (!config
->cluster_iam_master
)
1167 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1168 fmtaddr(config
->cluster_master_address
, 0));
1175 cli_error(cli
, "Specify a user to drop");
1179 for (i
= 0; i
< argc
; i
++)
1181 if (!(s
= sessionbyuser(argv
[i
])))
1183 cli_error(cli
, "User %s is not connected", argv
[i
]);
1187 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1189 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1190 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1197 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1202 if (CLI_HELP_REQUESTED
)
1203 return cli_arg_help(cli
, argc
> 1,
1204 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1206 if (!config
->cluster_iam_master
)
1208 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1209 fmtaddr(config
->cluster_master_address
, 0));
1216 cli_error(cli
, "Specify a tunnel to drop");
1220 for (i
= 0; i
< argc
; i
++)
1222 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1224 cli_error(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1230 cli_error(cli
, "Tunnel %d is not connected", t
);
1236 cli_error(cli
, "Tunnel %d is already being shut down", t
);
1240 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1241 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1247 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1252 if (CLI_HELP_REQUESTED
)
1253 return cli_arg_help(cli
, argc
> 1,
1254 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1256 if (!config
->cluster_iam_master
)
1258 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1259 fmtaddr(config
->cluster_master_address
, 0));
1266 cli_error(cli
, "Specify a session id to drop");
1270 for (i
= 0; i
< argc
; i
++)
1272 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1274 cli_error(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1278 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1280 cli_print(cli
, "Dropping session %d", s
);
1281 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1283 else if (session
[s
].forwardtosession
&& session
[s
].opened
&& !session
[s
].die
)
1285 cli_print(cli
, "Dropping session %d", s
);
1286 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1290 cli_error(cli
, "Session %d is not active.", s
);
1297 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1303 if (CLI_HELP_REQUESTED
)
1308 return cli_arg_help(cli
, 0,
1309 "USER", "Username of session to snoop", NULL
);
1312 return cli_arg_help(cli
, 0,
1313 "A.B.C.D", "IP address of snoop destination", NULL
);
1316 return cli_arg_help(cli
, 0,
1317 "N", "Port of snoop destination", NULL
);
1321 return cli_arg_help(cli
, 1, NULL
);
1328 if (!config
->cluster_iam_master
)
1330 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1331 fmtaddr(config
->cluster_master_address
, 0));
1338 cli_error(cli
, "Specify username, ip and port");
1342 if (!(s
= sessionbyuser(argv
[0])))
1344 cli_error(cli
, "User %s is not connected", argv
[0]);
1348 ip
= inet_addr(argv
[1]);
1349 if (!ip
|| ip
== INADDR_NONE
)
1351 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1355 port
= atoi(argv
[2]);
1358 cli_error(cli
, "Invalid port %s", argv
[2]);
1362 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], fmtaddr(ip
, 0), port
);
1363 cli_session_actions
[s
].snoop_ip
= ip
;
1364 cli_session_actions
[s
].snoop_port
= port
;
1365 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1370 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1375 if (CLI_HELP_REQUESTED
)
1376 return cli_arg_help(cli
, argc
> 1,
1377 "USER", "Username of session to unsnoop", NULL
);
1379 if (!config
->cluster_iam_master
)
1381 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1382 fmtaddr(config
->cluster_master_address
, 0));
1389 cli_error(cli
, "Specify a user to unsnoop");
1393 for (i
= 0; i
< argc
; i
++)
1395 if (!(s
= sessionbyuser(argv
[i
])))
1397 cli_error(cli
, "User %s is not connected", argv
[i
]);
1401 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1402 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1408 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1415 throttle USER - throttle in/out to default rate
1416 throttle USER RATE - throttle in/out to default rate
1417 throttle USER in RATE - throttle input only
1418 throttle USER out RATE - throttle output only
1419 throttle USER in RATE out RATE - throttle both
1422 if (CLI_HELP_REQUESTED
)
1427 return cli_arg_help(cli
, 0,
1428 "USER", "Username of session to throttle", NULL
);
1431 return cli_arg_help(cli
, 1,
1432 "RATE", "Rate in kbps (in and out)",
1433 "in", "Select incoming rate",
1434 "out", "Select outgoing rate", NULL
);
1437 return cli_arg_help(cli
, 1,
1438 "in", "Select incoming rate",
1439 "out", "Select outgoing rate", NULL
);
1442 if (isdigit(argv
[1][0]))
1443 return cli_arg_help(cli
, 1, NULL
);
1446 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1449 return cli_arg_help(cli
, argc
> 1, NULL
);
1453 if (!config
->cluster_iam_master
)
1455 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1456 fmtaddr(config
->cluster_master_address
, 0));
1463 cli_error(cli
, "Specify a user to throttle");
1467 if (!(s
= sessionbyuser(argv
[0])))
1469 cli_error(cli
, "User %s is not connected", argv
[0]);
1475 rate_in
= rate_out
= config
->rl_rate
;
1479 rate_in
= rate_out
= atoi(argv
[1]);
1482 cli_error(cli
, "Invalid rate \"%s\"", argv
[1]);
1486 else if (argc
== 3 || argc
== 5)
1489 for (i
= 1; i
< argc
- 1; i
+= 2)
1492 if (MATCH("in", argv
[i
]))
1493 r
= rate_in
= atoi(argv
[i
+1]);
1494 else if (MATCH("out", argv
[i
]))
1495 r
= rate_out
= atoi(argv
[i
+1]);
1499 cli_error(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1506 cli_error(cli
, "Invalid arguments");
1510 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1512 cli_error(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1516 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1517 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1518 cli_session_actions
[s
].throttle_in
= rate_in
;
1520 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1521 cli_session_actions
[s
].throttle_out
= rate_out
;
1523 if (cli_session_actions
[s
].throttle_in
== -1 &&
1524 cli_session_actions
[s
].throttle_out
== -1)
1526 cli_error(cli
, "User %s already throttled at this rate", argv
[0]);
1530 cli_print(cli
, "Throttling user %s", argv
[0]);
1531 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1536 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1541 if (CLI_HELP_REQUESTED
)
1542 return cli_arg_help(cli
, argc
> 1,
1543 "USER", "Username of session to unthrottle", NULL
);
1545 if (!config
->cluster_iam_master
)
1547 cli_error(cli
, "Can't do this on a slave. Do it on %s",
1548 fmtaddr(config
->cluster_master_address
, 0));
1555 cli_error(cli
, "Specify a user to unthrottle");
1559 for (i
= 0; i
< argc
; i
++)
1561 if (!(s
= sessionbyuser(argv
[i
])))
1563 cli_error(cli
, "User %s is not connected", argv
[i
]);
1567 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1569 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1570 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1574 cli_error(cli
, "User %s not throttled", argv
[i
]);
1581 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1585 if (CLI_HELP_REQUESTED
)
1586 return cli_arg_help(cli
, 1,
1587 "all", "Enable debugging for all except \"data\"",
1588 "critical", "", // FIXME: add descriptions
1598 char *p
= (char *) &debug_flags
;
1599 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1603 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1604 (debug_flags
.critical
) ? " critical" : "",
1605 (debug_flags
.error
) ? " error" : "",
1606 (debug_flags
.warning
) ? " warning" : "",
1607 (debug_flags
.info
) ? " info" : "",
1608 (debug_flags
.calls
) ? " calls" : "",
1609 (debug_flags
.data
) ? " data" : "");
1615 cli_print(cli
, "Debugging off");
1619 for (i
= 0; i
< argc
; i
++)
1621 int len
= strlen(argv
[i
]);
1623 if (argv
[i
][0] == 'c' && len
< 2)
1624 len
= 2; /* distinguish [cr]itical from [ca]lls */
1626 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 1; continue; }
1627 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 1; continue; }
1628 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 1; continue; }
1629 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 1; continue; }
1630 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 1; continue; }
1631 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 1; continue; }
1632 if (!strncmp("all", argv
[i
], len
))
1634 memset(&debug_flags
, 1, sizeof(debug_flags
));
1635 debug_flags
.data
= 0;
1639 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1645 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1649 if (CLI_HELP_REQUESTED
)
1650 return cli_arg_help(cli
, 1,
1651 "all", "Disable all debugging",
1652 "critical", "", // FIXME: add descriptions
1662 memset(&debug_flags
, 0, sizeof(debug_flags
));
1666 for (i
= 0; i
< argc
; i
++)
1668 int len
= strlen(argv
[i
]);
1670 if (argv
[i
][0] == 'c' && len
< 2)
1671 len
= 2; /* distinguish [cr]itical from [ca]lls */
1673 if (!strncmp("critical", argv
[i
], len
)) { debug_flags
.critical
= 0; continue; }
1674 if (!strncmp("error", argv
[i
], len
)) { debug_flags
.error
= 0; continue; }
1675 if (!strncmp("warning", argv
[i
], len
)) { debug_flags
.warning
= 0; continue; }
1676 if (!strncmp("info", argv
[i
], len
)) { debug_flags
.info
= 0; continue; }
1677 if (!strncmp("calls", argv
[i
], len
)) { debug_flags
.calls
= 0; continue; }
1678 if (!strncmp("data", argv
[i
], len
)) { debug_flags
.data
= 0; continue; }
1679 if (!strncmp("all", argv
[i
], len
))
1681 memset(&debug_flags
, 0, sizeof(debug_flags
));
1685 cli_error(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1691 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1693 int i
, firstfree
= 0;
1695 if (CLI_HELP_REQUESTED
)
1696 return cli_arg_help(cli
, argc
> 1,
1697 "PLUGIN", "Name of plugin to load", NULL
);
1701 cli_error(cli
, "Specify a plugin to load");
1705 for (i
= 0; i
< MAXPLUGINS
; i
++)
1707 if (!*config
->plugins
[i
] && !firstfree
)
1709 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1711 cli_error(cli
, "Plugin is already loaded");
1718 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1719 config
->reload_config
= 1;
1720 cli_print(cli
, "Loading plugin %s", argv
[0]);
1726 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1730 if (CLI_HELP_REQUESTED
)
1731 return cli_arg_help(cli
, argc
> 1,
1732 "PLUGIN", "Name of plugin to unload", NULL
);
1736 cli_error(cli
, "Specify a plugin to remove");
1740 for (i
= 0; i
< MAXPLUGINS
; i
++)
1742 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1744 config
->reload_config
= 1;
1745 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1750 cli_error(cli
, "Plugin is not loaded");
1754 static char *duration(time_t secs
)
1756 static char *buf
= NULL
;
1759 if (!buf
) buf
= calloc(64, 1);
1763 int days
= secs
/ 86400;
1764 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1770 int mins
= secs
/ 60;
1771 int hrs
= mins
/ 60;
1774 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1776 else if (secs
>= 60)
1778 int mins
= secs
/ 60;
1779 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1782 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1787 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1790 char buf
[100], *p
= buf
, *loads
[3];
1791 int i
, num_sessions
= 0;
1793 if (CLI_HELP_REQUESTED
)
1794 return CLI_HELP_NO_ARGS
;
1796 fh
= fopen("/proc/loadavg", "r");
1797 fgets(buf
, 100, fh
);
1800 for (i
= 0; i
< 3; i
++)
1801 loads
[i
] = strdup(strsep(&p
, " "));
1804 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1806 for (i
= 1; i
< MAXSESSION
; i
++)
1807 if (session
[i
].opened
) num_sessions
++;
1809 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1811 duration(time_now
- config
->start_time
),
1813 loads
[0], loads
[1], loads
[2]
1815 for (i
= 0; i
< 3; i
++)
1816 if (loads
[i
]) free(loads
[i
]);
1818 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1823 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1827 if (CLI_HELP_REQUESTED
)
1833 int len
= strlen(argv
[0])-1;
1834 for (i
= 0; config_values
[i
].key
; i
++)
1835 if (!len
|| !strncmp(config_values
[i
].key
, argv
[0], len
))
1836 cli_error(cli
, " %s", config_values
[i
].key
);
1842 return cli_arg_help(cli
, 0,
1843 "VALUE", "Value for variable", NULL
);
1847 return cli_arg_help(cli
, 1, NULL
);
1856 cli_error(cli
, "Specify variable and value");
1860 for (i
= 0; config_values
[i
].key
; i
++)
1862 void *value
= ((void *) config
) + config_values
[i
].offset
;
1863 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1865 // Found a value to set
1866 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1867 switch (config_values
[i
].type
)
1870 snprintf((char *) value
, config_values
[i
].size
, "%s", argv
[1]);
1873 *(int *) value
= atoi(argv
[1]);
1876 *(unsigned long *) value
= atol(argv
[1]);
1879 *(short *) value
= atoi(argv
[1]);
1882 *(in_addr_t
*) value
= inet_addr(argv
[1]);
1885 inet_pton(AF_INET6
, argv
[1], value
);
1888 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1894 cli_error(cli
, "Unknown variable type");
1897 config
->reload_config
= 1;
1902 cli_error(cli
, "Unknown variable \"%s\"", argv
[0]);
1906 int regular_stuff(struct cli_def
*cli
)
1912 for (i
= debug_rb_tail
; i
!= ringbuffer
->tail
; i
= (i
+ 1) % RINGBUFFER_SIZE
)
1914 char *m
= ringbuffer
->buffer
[i
].message
;
1920 switch (ringbuffer
->buffer
[i
].level
)
1922 case 0: show
= debug_flags
.critical
; break;
1923 case 1: show
= debug_flags
.error
; break;
1924 case 2: show
= debug_flags
.warning
; break;
1925 case 3: show
= debug_flags
.info
; break;
1926 case 4: show
= debug_flags
.calls
; break;
1927 case 5: show
= debug_flags
.data
; break;
1930 if (!show
) continue;
1932 if (!(p
= strchr(m
, '\n')))
1935 cli_error(cli
, "\r%s-%u-%u %.*s",
1936 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1937 ringbuffer
->buffer
[i
].tunnel
,
1938 ringbuffer
->buffer
[i
].session
,
1944 debug_rb_tail
= ringbuffer
->tail
;
1952 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1956 if (CLI_HELP_REQUESTED
)
1957 return cli_arg_help(cli
, argc
> 1,
1958 "<1-65535>", "Autonomous system number", NULL
);
1960 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1962 cli_error(cli
, "Invalid autonomous system number");
1966 if (bgp_configured
&& as
!= config
->as_number
)
1968 cli_error(cli
, "Can't change local AS on a running system");
1972 config
->as_number
= as
;
1973 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1978 static int find_bgp_neighbour(char const *name
)
1983 in_addr_t addrs
[4] = { 0 };
1986 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1989 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1990 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1992 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1994 if (!config
->neighbour
[i
].name
[0])
1996 if (new == -1) new = i
;
2000 if (!strcmp(name
, config
->neighbour
[i
].name
))
2003 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
2006 for (a
= h
->h_addr_list
; *a
; a
++)
2009 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
2010 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
2018 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2024 if (CLI_HELP_REQUESTED
)
2029 return cli_arg_help(cli
, 0,
2030 "A.B.C.D", "BGP neighbour address",
2031 "NAME", "BGP neighbour name",
2035 return cli_arg_help(cli
, 0,
2036 "remote-as", "Set remote autonomous system number",
2037 "timers", "Set timers",
2038 "update-source", "Set source address to use for the BGP session",
2042 if (MATCH("remote-as", argv
[1]))
2043 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
2045 if (MATCH("timers", argv
[1]))
2048 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
2051 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
2053 if (argc
== 5 && !argv
[4][1])
2054 return cli_arg_help(cli
, 1, NULL
);
2057 if (MATCH("update-source", argv
[1]))
2058 return cli_arg_help(cli
, argc
> 3, "A.B.C.D", "Source IP address", NULL
);
2066 cli_error(cli
, "Invalid arguments");
2070 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2072 cli_error(cli
, "Invalid neighbour");
2078 cli_error(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2082 if (MATCH("remote-as", argv
[1]))
2084 int as
= atoi(argv
[2]);
2085 if (as
< 0 || as
> 65535)
2087 cli_error(cli
, "Invalid autonomous system number");
2091 if (!config
->neighbour
[i
].name
[0])
2093 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), "%s", argv
[0]);
2094 config
->neighbour
[i
].keepalive
= -1;
2095 config
->neighbour
[i
].hold
= -1;
2096 config
->neighbour
[i
].update_source
.s_addr
= INADDR_ANY
;
2099 config
->neighbour
[i
].as
= as
;
2103 if (MATCH("update-source", argv
[1]))
2105 struct in_addr addr
;
2107 if (!config
->neighbour
[i
].name
[0])
2109 cli_error(cli
, "Specify remote-as first");
2113 if (!inet_aton(argv
[2], &addr
))
2115 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[2]);
2119 config
->neighbour
[i
].update_source
= addr
;
2123 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2125 cli_error(cli
, "Invalid arguments");
2129 if (!config
->neighbour
[i
].name
[0])
2131 cli_error(cli
, "Specify remote-as first");
2135 keepalive
= atoi(argv
[2]);
2136 hold
= atoi(argv
[3]);
2138 if (keepalive
< 1 || keepalive
> 65535)
2140 cli_error(cli
, "Invalid keepalive time");
2144 if (hold
< 3 || hold
> 65535)
2146 cli_error(cli
, "Invalid hold time");
2150 if (keepalive
== BGP_KEEPALIVE_TIME
)
2151 keepalive
= -1; // using default value
2153 if (hold
== BGP_HOLD_TIME
)
2156 config
->neighbour
[i
].keepalive
= keepalive
;
2157 config
->neighbour
[i
].hold
= hold
;
2162 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2166 if (CLI_HELP_REQUESTED
)
2167 return cli_arg_help(cli
, argc
> 0,
2168 "A.B.C.D", "BGP neighbour address",
2169 "NAME", "BGP neighbour name",
2174 cli_error(cli
, "Specify a BGP neighbour");
2178 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2180 cli_error(cli
, "Invalid neighbour");
2184 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2186 cli_error(cli
, "Neighbour %s not configured", argv
[0]);
2190 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2194 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2200 if (!bgp_configured
)
2203 if (CLI_HELP_REQUESTED
)
2204 return cli_arg_help(cli
, 1,
2205 "A.B.C.D", "BGP neighbour address",
2206 "NAME", "BGP neighbour name",
2209 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2210 BGP_VERSION
, fmtaddr(my_address
, 0), (int) config
->as_number
);
2214 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2216 if (!*bgp_peers
[i
].name
)
2219 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2220 if (argc
&& strcmp(addr
, argv
[0]) &&
2221 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2227 cli_print(cli
, "Peer AS Address "
2228 "State Retries Retry in Route Pend Timers");
2229 cli_print(cli
, "------------------ ----- --------------- "
2230 "----------- ------- -------- ----- ---- ---------");
2233 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2237 bgp_state_str(bgp_peers
[i
].state
),
2238 bgp_peers
[i
].retry_count
,
2239 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2240 bgp_peers
[i
].routing
? "yes" : "no",
2241 bgp_peers
[i
].update_routes
? "yes" : "no",
2242 bgp_peers
[i
].keepalive
,
2249 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2254 if (!bgp_configured
)
2257 if (CLI_HELP_REQUESTED
)
2258 return cli_arg_help(cli
, 1,
2259 "A.B.C.D", "BGP neighbour address",
2260 "NAME", "BGP neighbour name",
2263 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2265 if (bgp_peers
[i
].state
!= Established
)
2268 if (!bgp_peers
[i
].routing
)
2271 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2272 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2275 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2276 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2282 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2287 if (!bgp_configured
)
2290 if (CLI_HELP_REQUESTED
)
2291 return cli_arg_help(cli
, 1,
2292 "A.B.C.D", "BGP neighbour address",
2293 "NAME", "BGP neighbour name",
2296 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2298 if (bgp_peers
[i
].state
!= Established
)
2301 if (bgp_peers
[i
].routing
)
2304 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2305 if (argc
&& strcmp(addr
, argv
[0]) &&
2306 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2309 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2310 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2316 static int cmd_restart_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
].name
)
2335 addr
= fmtaddr(bgp_peers
[i
].addr
, 0);
2336 if (argc
&& strcmp(addr
, argv
[0]) &&
2337 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2340 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2341 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2349 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2353 if (CLI_HELP_REQUESTED
)
2358 return cli_arg_help(cli
, 0,
2359 "standard", "Standard syntax",
2360 "extended", "Extended syntax",
2364 return cli_arg_help(cli
, argv
[1][1],
2365 "NAME", "Access-list name",
2369 if (argc
== 3 && !argv
[2][1])
2370 return cli_arg_help(cli
, 1, NULL
);
2378 cli_error(cli
, "Specify access-list type and name");
2382 if (MATCH("standard", argv
[0]))
2384 else if (MATCH("extended", argv
[0]))
2388 cli_error(cli
, "Invalid access-list type");
2392 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2393 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2395 cli_error(cli
, "Invalid access-list name");
2399 filt
= find_filter(argv
[1], strlen(argv
[1]));
2404 cli_error(cli
, "Too many access-lists");
2409 if (!*ip_filters
[filt
].name
)
2411 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2412 strcpy(ip_filters
[filt
].name
, argv
[1]);
2413 ip_filters
[filt
].extended
= extended
;
2415 else if (ip_filters
[filt
].extended
!= extended
)
2417 cli_error(cli
, "Access-list is %s",
2418 ip_filters
[filt
].extended
? "extended" : "standard");
2423 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2427 if (filt
< 0 || !*ip_filters
[filt
].name
)
2429 cli_error(cli
, "Access-list not defined");
2434 if (ip_filters
[filt
].used
)
2436 cli_error(cli
, "Access-list in use");
2440 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2444 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2446 return access_list(cli
, argv
, argc
, 1);
2449 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2451 return access_list(cli
, argv
, argc
, 0);
2454 static int show_ip_wild(char *buf
, in_addr_t ip
, in_addr_t wild
)
2456 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2457 return sprintf(buf
, " any");
2459 if (wild
== INADDR_ANY
)
2460 return sprintf(buf
, " host %s", fmtaddr(ip
, 0));
2462 return sprintf(buf
, " %s %s", fmtaddr(ip
, 0), fmtaddr(wild
, 1));
2465 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2469 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2470 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2471 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2472 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2473 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2479 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2481 static char buf
[256];
2484 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2487 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2488 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2491 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2495 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2496 p
+= show_ports(p
, &rule
->src_ports
);
2498 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2499 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2500 p
+= show_ports(p
, &rule
->dst_ports
);
2502 if (rule
->proto
== IPPROTO_TCP
&& rule
->tcp_flag_op
)
2504 switch (rule
->tcp_flag_op
)
2506 case FILTER_FLAG_OP_EST
:
2507 p
+= sprintf(p
, " established");
2510 case FILTER_FLAG_OP_ANY
:
2511 case FILTER_FLAG_OP_ALL
:
2512 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2513 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2514 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2515 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2516 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2517 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2518 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2519 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2520 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2521 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2522 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2523 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2524 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2530 p
+= sprintf(p
, " fragments");
2535 static ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2537 static ip_filter_rulet rule
;
2538 struct in_addr addr
;
2542 if (CLI_HELP_REQUESTED
)
2546 cli_arg_help(cli
, 0,
2547 "ip", "Match IP packets",
2548 "tcp", "Match TCP packets",
2549 "udp", "Match UDP packets",
2555 // *sigh*, too darned complex
2556 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2562 cli_error(cli
, "Specify rule details");
2566 memset(&rule
, 0, sizeof(rule
));
2567 rule
.action
= (command
[0] == 'p')
2568 ? FILTER_ACTION_PERMIT
2569 : FILTER_ACTION_DENY
;
2571 if (MATCH("ip", argv
[0]))
2572 rule
.proto
= IPPROTO_IP
;
2573 else if (MATCH("udp", argv
[0]))
2574 rule
.proto
= IPPROTO_UDP
;
2575 else if (MATCH("tcp", argv
[0]))
2576 rule
.proto
= IPPROTO_TCP
;
2579 cli_error(cli
, "Invalid protocol \"%s\"", argv
[0]);
2583 for (a
= 1, i
= 0; i
< 2; i
++)
2587 ip_filter_portt
*port
;
2592 wild
= &rule
.src_wild
;
2593 port
= &rule
.src_ports
;
2598 wild
= &rule
.dst_wild
;
2599 port
= &rule
.dst_ports
;
2602 cli_error(cli
, "Specify destination");
2607 if (MATCH("any", argv
[a
]))
2610 *wild
= INADDR_BROADCAST
;
2613 else if (MATCH("host", argv
[a
]))
2617 cli_error(cli
, "Specify host ip address");
2621 if (!inet_aton(argv
[a
], &addr
))
2623 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2635 cli_error(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2639 if (!inet_aton(argv
[a
], &addr
))
2641 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2647 if (!inet_aton(argv
[++a
], &addr
))
2649 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2653 *wild
= addr
.s_addr
;
2657 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2661 if (MATCH("eq", argv
[a
]))
2662 port
->op
= FILTER_PORT_OP_EQ
;
2663 else if (MATCH("neq", argv
[a
]))
2664 port
->op
= FILTER_PORT_OP_NEQ
;
2665 else if (MATCH("gt", argv
[a
]))
2666 port
->op
= FILTER_PORT_OP_GT
;
2667 else if (MATCH("lt", argv
[a
]))
2668 port
->op
= FILTER_PORT_OP_LT
;
2669 else if (MATCH("range", argv
[a
]))
2670 port
->op
= FILTER_PORT_OP_RANGE
;
2677 cli_error(cli
, "Specify port");
2681 if (!(port
->port
= atoi(argv
[a
])))
2683 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2688 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2693 cli_error(cli
, "Specify port");
2697 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2699 cli_error(cli
, "Invalid port \"%s\"", argv
[a
]);
2706 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2708 if (MATCH("established", argv
[a
]))
2710 rule
.tcp_flag_op
= FILTER_FLAG_OP_EST
;
2713 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2714 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2716 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2717 ? FILTER_FLAG_OP_ANY
2718 : FILTER_FLAG_OP_ALL
;
2722 cli_error(cli
, "Specify tcp flags");
2726 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2730 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2732 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2733 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2734 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2735 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2736 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2737 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2740 cli_error(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2749 if (a
< argc
&& MATCH("fragments", argv
[a
]))
2751 if (rule
.src_ports
.op
|| rule
.dst_ports
.op
|| rule
.tcp_flag_op
)
2753 cli_error(cli
, "Can't specify \"fragments\" on rules with layer 4 matches");
2763 cli_error(cli
, "Invalid flag \"%s\"", argv
[a
]);
2770 static ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2772 static ip_filter_rulet rule
;
2773 struct in_addr addr
;
2775 if (CLI_HELP_REQUESTED
)
2779 cli_arg_help(cli
, argv
[0][1],
2780 "A.B.C.D", "Source address",
2781 "any", "Any source address",
2782 "host", "Source host",
2788 if (MATCH("any", argv
[0]))
2790 if (argc
== 2 && !argv
[1][1])
2791 cli_arg_help(cli
, 1, NULL
);
2793 else if (MATCH("host", argv
[0]))
2797 cli_arg_help(cli
, argv
[1][1],
2798 "A.B.C.D", "Host address",
2801 else if (argc
== 3 && !argv
[2][1])
2802 cli_arg_help(cli
, 1, NULL
);
2808 cli_arg_help(cli
, 1,
2809 "A.B.C.D", "Wildcard bits",
2812 else if (argc
== 3 && !argv
[2][1])
2813 cli_arg_help(cli
, 1, NULL
);
2821 cli_error(cli
, "Specify rule details");
2825 memset(&rule
, 0, sizeof(rule
));
2826 rule
.action
= (command
[0] == 'p')
2827 ? FILTER_ACTION_PERMIT
2828 : FILTER_ACTION_DENY
;
2830 rule
.proto
= IPPROTO_IP
;
2831 if (MATCH("any", argv
[0]))
2833 rule
.src_ip
= INADDR_ANY
;
2834 rule
.src_wild
= INADDR_BROADCAST
;
2836 else if (MATCH("host", argv
[0]))
2840 cli_error(cli
, "Specify host ip address");
2844 if (!inet_aton(argv
[1], &addr
))
2846 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2850 rule
.src_ip
= addr
.s_addr
;
2851 rule
.src_wild
= INADDR_ANY
;
2857 cli_error(cli
, "Specify source ip address and wildcard");
2861 if (!inet_aton(argv
[0], &addr
))
2863 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2867 rule
.src_ip
= addr
.s_addr
;
2871 if (!inet_aton(argv
[1], &addr
))
2873 cli_error(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2877 rule
.src_wild
= addr
.s_addr
;
2880 rule
.src_wild
= INADDR_ANY
;
2886 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2889 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2890 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2891 : access_list_rule_std(cli
, command
, argv
, argc
);
2896 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2898 if (!ip_filters
[filt
].rules
[i
].action
)
2900 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2904 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, offsetof(ip_filter_rulet
, counter
)))
2908 cli_error(cli
, "Too many rules");
2912 static int cmd_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2917 /* filter USER {in|out} FILTER ... */
2918 if (CLI_HELP_REQUESTED
)
2923 return cli_arg_help(cli
, 0,
2924 "USER", "Username of session to filter", NULL
);
2928 return cli_arg_help(cli
, 0,
2929 "in", "Set incoming filter",
2930 "out", "Set outgoing filter", NULL
);
2934 return cli_arg_help(cli
, argc
== 5 && argv
[4][1],
2935 "NAME", "Filter name", NULL
);
2938 return cli_arg_help(cli
, argc
> 1, NULL
);
2942 if (!config
->cluster_iam_master
)
2944 cli_error(cli
, "Can't do this on a slave. Do it on %s",
2945 fmtaddr(config
->cluster_master_address
, 0));
2950 if (argc
!= 3 && argc
!= 5)
2952 cli_error(cli
, "Specify a user and filters");
2956 if (!(s
= sessionbyuser(argv
[0])))
2958 cli_error(cli
, "User %s is not connected", argv
[0]);
2962 cli_session_actions
[s
].filter_in
= cli_session_actions
[s
].filter_out
= -1;
2963 for (i
= 1; i
< argc
; i
+= 2)
2968 if (MATCH("in", argv
[i
]))
2970 if (session
[s
].filter_in
)
2972 cli_error(cli
, "Input already filtered");
2975 f
= &cli_session_actions
[s
].filter_in
;
2977 else if (MATCH("out", argv
[i
]))
2979 if (session
[s
].filter_out
)
2981 cli_error(cli
, "Output already filtered");
2984 f
= &cli_session_actions
[s
].filter_out
;
2988 cli_error(cli
, "Invalid filter specification");
2992 v
= find_filter(argv
[i
+1], strlen(argv
[i
+1]));
2993 if (v
< 0 || !*ip_filters
[v
].name
)
2995 cli_error(cli
, "Access-list %s not defined", argv
[i
+1]);
3002 cli_print(cli
, "Filtering user %s", argv
[0]);
3003 cli_session_actions
[s
].action
|= CLI_SESS_FILTER
;
3008 static int cmd_no_filter(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3013 if (CLI_HELP_REQUESTED
)
3014 return cli_arg_help(cli
, argc
> 1,
3015 "USER", "Username of session to remove filters from", NULL
);
3017 if (!config
->cluster_iam_master
)
3019 cli_error(cli
, "Can't do this on a slave. Do it on %s",
3020 fmtaddr(config
->cluster_master_address
, 0));
3027 cli_error(cli
, "Specify a user to remove filters from");
3031 for (i
= 0; i
< argc
; i
++)
3033 if (!(s
= sessionbyuser(argv
[i
])))
3035 cli_error(cli
, "User %s is not connected", argv
[i
]);
3039 if (session
[s
].filter_in
|| session
[s
].filter_out
)
3041 cli_print(cli
, "Removing filters from user %s", argv
[i
]);
3042 cli_session_actions
[s
].action
|= CLI_SESS_NOFILTER
;
3046 cli_error(cli
, "User %s not filtered", argv
[i
]);
3053 static int cmd_show_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3057 if (CLI_HELP_REQUESTED
)
3058 return cli_arg_help(cli
, argc
> 1, "NAME", "Filter name", NULL
);
3062 cli_error(cli
, "Specify a filter name");
3066 for (i
= 0; i
< argc
; i
++)
3068 int f
= find_filter(argv
[i
], strlen(argv
[i
]));
3069 ip_filter_rulet
*rules
;
3071 if (f
< 0 || !*ip_filters
[f
].name
)
3073 cli_error(cli
, "Access-list %s not defined", argv
[i
]);
3078 cli_print(cli
, " ");
3080 cli_print(cli
, "%s IP access list %s",
3081 ip_filters
[f
].extended
? "Extended" : "Standard",
3082 ip_filters
[f
].name
);
3084 for (rules
= ip_filters
[f
].rules
; rules
->action
; rules
++)
3086 char const *r
= show_access_list_rule(ip_filters
[f
].extended
, rules
);
3088 cli_print(cli
, "%s (%u match%s)", r
,
3089 rules
->counter
, rules
->counter
> 1 ? "es" : "");
3091 cli_print(cli
, "%s", r
);
3098 static int cmd_shutdown(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3100 if (CLI_HELP_REQUESTED
)
3101 return CLI_HELP_NO_ARGS
;
3103 kill(getppid(), SIGQUIT
);
3107 static int cmd_reload(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3109 if (CLI_HELP_REQUESTED
)
3110 return CLI_HELP_NO_ARGS
;
3112 kill(getppid(), SIGHUP
);
3116 static int cmd_show_group(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3121 if (CLI_HELP_REQUESTED
)
3122 return cli_arg_help(cli
, 1,
3123 "<1-%d>", MAXGROUPE
-1, "Show specific group by id",
3129 // Show individual group
3130 for (i
= 0; i
< argc
; i
++)
3135 if (g
<= 0 || g
>= MAXGROUPE
)
3137 cli_print(cli
, "Invalid group id \"%s\"", argv
[i
]);
3141 cli_print(cli
, "\r\nGroup %d:", g
);
3142 cli_print(cli
, "\tNb Session:\t\t%d", grpsession
[g
].nbsession
);
3143 cli_print(cli
, "\tNb Routes:\t\t%d", grpsession
[g
].nbroutesgrp
);
3144 cli_print(cli
, "\ttime_changed:\t\t%d\n", grpsession
[g
].time_changed
);
3146 for (i
= 0; i
< grpsession
[g
].nbsession
; i
++)
3148 if ((s
= grpsession
[g
].sesslist
[i
].sid
))
3150 cli_print(cli
, "\tSession:\t%d\tTx Rate:%d Kbps\t\t\tweight:\t%d",
3152 grpsession
[g
].sesslist
[i
].tx_rate
/(1024/8),
3153 grpsession
[g
].sesslist
[i
].weight
);
3157 for (i
= 0; i
< grpsession
[g
].nbroutesgrp
; i
++)
3159 if (grpsession
[g
].route
[i
].ip
!= 0)
3161 cli_print(cli
, "\tRoute:\t%s/%d", fmtaddr(htonl(grpsession
[g
].route
[i
].ip
), 0), grpsession
[g
].route
[i
].prefixlen
);
3169 cli_print(cli
, "%5s %7s %9s %12s",
3175 for (g
= gnextgrpid
; g
!= 0; g
= grpsession
[g
].prev
)
3177 cli_print(cli
, "%5d %7d %9d %12d",
3179 grpsession
[g
].nbsession
,
3180 grpsession
[g
].nbroutesgrp
,
3181 grpsession
[g
].time_changed
);
3187 static int cmd_setforward(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3191 if (CLI_HELP_REQUESTED
)
3196 return cli_arg_help(cli
, 0,
3197 "MASK", "Users mask to forward (ex: myISP@operator.com)", NULL
);
3200 return cli_arg_help(cli
, 0,
3201 "IP", "IP of the remote LNS(ex: 64.64.64.64)", NULL
);
3204 return cli_arg_help(cli
, 0,
3205 "PORT", "Port of the remote LNS (ex: 1701)", NULL
);
3208 return cli_arg_help(cli
, 0,
3209 "SECRET", "l2tp secret of the remote LNS (ex: mysecretpsw)", NULL
);
3212 return cli_arg_help(cli
, argc
> 1, NULL
);
3218 cli_error(cli
, "Specify variable and value");
3222 // lac_addremotelns(mask, IP_RemoteLNS, Port_RemoteLNS, SecretRemoteLNS)
3223 ret
= lac_addremotelns(argv
[0], argv
[1], argv
[2], argv
[3]);
3227 cli_print(cli
, "setforward %s %s %s %s", argv
[0], argv
[1], argv
[2], argv
[3]);
3229 cli_print(cli
, "%s Updated, the tunnel must be dropped", argv
[0]);
3232 cli_error(cli
, "ERROR setforward %s %s %s %s", argv
[0], argv
[1], argv
[2], argv
[3]);
3237 static int cmd_show_rmtlnsconf(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
3242 if (CLI_HELP_REQUESTED
)
3244 return cli_arg_help(cli
, 0, "remotelns-conf", "Show a list of remote LNS configurations", NULL
);
3247 for (idrlns
= 0; idrlns
< MAXRLNSTUNNEL
; idrlns
++)
3249 if (lac_cli_show_remotelns(idrlns
, strdisp
) != 0)
3250 cli_print(cli
, "%s", strdisp
);