1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.31 2004-11-27 21:10:50 bodea Exp $";
17 #include <arpa/inet.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
35 extern tunnelt
*tunnel
;
36 extern sessiont
*session
;
37 extern radiust
*radius
;
38 extern ippoolt
*ip_address_pool
;
39 extern struct Tstats
*_statistics
;
40 static struct cli_def
*cli
= NULL
;
41 extern configt
*config
;
42 extern config_descriptt config_values
[];
44 extern struct Tringbuffer
*ringbuffer
;
46 extern struct cli_session_actions
*cli_session_actions
;
47 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
48 extern tbft
*filter_list
;
49 extern ip_filtert
*ip_filters
;
51 static char *debug_levels
[] = {
70 static int debug_session
;
71 static int debug_tunnel
;
72 static int debug_rb_tail
;
74 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
75 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 static int regular_stuff(struct cli_def
*cli
);
101 static void parsemac(char *string
, char mac
[6]);
104 #define MODE_CONFIG_BGP 8
105 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
106 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
107 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
108 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
109 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
110 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
111 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
114 #define MODE_CONFIG_NACL 9
115 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
116 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
117 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
119 /* match if b is a substr of a */
120 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
122 void init_cli(char *hostname
)
126 struct cli_command
*c
;
127 struct cli_command
*c2
;
129 struct sockaddr_in addr
;
132 if (hostname
&& *hostname
)
133 cli_set_hostname(cli
, hostname
);
135 cli_set_hostname(cli
, "l2tpns");
137 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
138 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
140 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
142 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
143 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
144 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
145 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
146 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
147 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
148 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
149 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
150 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
151 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
152 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");
153 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
155 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
156 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
157 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
160 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
162 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
163 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
166 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
168 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
169 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
170 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
172 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable interception for a user");
173 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable throttling for a user");
174 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
177 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
178 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP neighbour");
181 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
182 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable interception for a user");
183 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable throttling for a user");
184 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
187 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
188 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP neighbour");
190 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
191 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
193 c
= cli_register_command(cli
, NULL
, "router", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
194 cli_register_command(cli
, c
, "bgp", cmd_router_bgp
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Configure BGP");
196 cli_register_command(cli
, NULL
, "neighbour", cmd_router_bgp_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Configure BGP neighbour");
198 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, NULL
);
199 cli_register_command(cli
, c
, "neighbour", cmd_router_bgp_no_neighbour
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_BGP
, "Remove BGP neighbour");
202 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
203 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
204 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
205 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
207 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
208 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
210 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
211 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
213 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
215 c
= cli_register_command(cli
, NULL
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
216 cli_register_command(cli
, c
, "access-list", cmd_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Add named access-list");
218 cli_register_command(cli
, NULL
, "permit", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Permit rule");
219 cli_register_command(cli
, NULL
, "deny", cmd_ip_access_list_rule
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG_NACL
, "Deny rule");
221 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_CONFIG
, NULL
);
222 c2
= cli_register_command(cli
, c
, "ip", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
223 cli_register_command(cli
, c2
, "access-list", cmd_no_ip_access_list
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove named access-list");
225 // Enable regular processing
226 cli_regular(cli
, regular_stuff
);
228 if (!(f
= fopen(CLIUSERS
, "r")))
230 LOG(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
234 while (fgets(buf
, 4096, f
))
237 if (*buf
== '#') continue;
238 if ((p
= strchr(buf
, '\r'))) *p
= 0;
239 if ((p
= strchr(buf
, '\n'))) *p
= 0;
241 if (!(p
= strchr((char *)buf
, ':'))) continue;
243 if (!strcmp(buf
, "enable"))
245 cli_allow_enable(cli
, p
);
246 LOG(3, 0, 0, 0, "Setting enable password\n");
250 cli_allow_user(cli
, buf
, p
);
251 LOG(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
257 memset(&addr
, 0, sizeof(addr
));
258 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
259 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
262 // Set cli fd as non-blocking
263 flags
= fcntl(clifd
, F_GETFL
, 0);
264 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
266 addr
.sin_family
= AF_INET
;
267 addr
.sin_port
= htons(23);
268 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
270 LOG(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
276 void cli_do(int sockfd
)
278 int require_auth
= 1;
279 struct sockaddr_in addr
;
280 int l
= sizeof(addr
);
282 if (fork_and_close()) return;
283 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
285 LOG(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
286 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
289 LOG(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
293 LOG(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
294 if (!cli
->users
) /* paranoia */
296 LOG(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
302 /* no username/pass required */
309 debug_rb_tail
= ringbuffer
->tail
;
311 memset(&debug_flags
, 0, sizeof(debug_flags
));
312 debug_flags
.critical
= 1;
314 cli_loop(cli
, sockfd
);
317 LOG(3, 0, 0, 0, "Closed CLI connection from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
321 static void cli_print_log(struct cli_def
*cli
, char *string
)
323 LOG(3, 0, 0, 0, "%s\n", string
);
326 void cli_do_file(FILE *fh
)
328 LOG(3, 0, 0, 0, "Reading configuration file\n");
329 cli_print_callback(cli
, cli_print_log
);
330 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
331 cli_print_callback(cli
, NULL
);
334 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
345 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
347 int v
= va_arg(ap
, int);
348 snprintf(buf
, sizeof(buf
), entry
, v
);
354 desc
= va_arg(ap
, char *);
356 cli_print(cli
, " %-20s %s", p
, desc
);
358 cli_print(cli
, " %s", p
);
360 entry
= desc
? va_arg(ap
, char *) : 0;
365 cli_print(cli
, " <cr>");
370 static int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
374 if (CLI_HELP_REQUESTED
)
375 return cli_arg_help(cli
, 1,
376 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
382 // Show individual session
383 for (i
= 0; i
< argc
; i
++)
385 unsigned int s
, b_in
, b_out
;
387 if (s
<= 0 || s
>= MAXSESSION
)
389 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
392 cli_print(cli
, "\r\nSession %d:", s
);
393 cli_print(cli
, "\tUser:\t\t%s", session
[s
].user
[0] ? session
[s
].user
: "none");
394 cli_print(cli
, "\tCalling Num:\t%s", session
[s
].calling
);
395 cli_print(cli
, "\tCalled Num:\t%s", session
[s
].called
);
396 cli_print(cli
, "\tTunnel ID:\t%d", session
[s
].tunnel
);
397 cli_print(cli
, "\tIP address:\t%s", inet_toa(htonl(session
[s
].ip
)));
398 cli_print(cli
, "\tUnique SID:\t%lu", session
[s
].unique_id
);
399 cli_print(cli
, "\tIdle time:\t%u seconds", abs(time_now
- session
[s
].last_packet
));
400 cli_print(cli
, "\tNext Recv:\t%u", session
[s
].nr
);
401 cli_print(cli
, "\tNext Send:\t%u", session
[s
].ns
);
402 cli_print(cli
, "\tBytes In/Out:\t%lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
403 cli_print(cli
, "\tPkts In/Out:\t%lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
404 cli_print(cli
, "\tMRU:\t\t%d", session
[s
].mru
);
405 cli_print(cli
, "\tRadius Session:\t%u", session
[s
].radius
);
406 cli_print(cli
, "\tRx Speed:\t%lu", session
[s
].rx_connect_speed
);
407 cli_print(cli
, "\tTx Speed:\t%lu", session
[s
].tx_connect_speed
);
408 if (session
[s
].filter_in
&& session
[s
].filter_in
<= MAXFILTER
)
409 cli_print(cli
, "\tFilter in:\t%u (%s)", session
[s
].filter_in
, ip_filters
[session
[s
].filter_in
-1].name
);
410 if (session
[s
].filter_out
&& session
[s
].filter_out
<= MAXFILTER
)
411 cli_print(cli
, "\tFilter out:\t%u (%s)", session
[s
].filter_out
, ip_filters
[session
[s
].filter_out
-1].name
);
412 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
413 cli_print(cli
, "\tIntercepted:\t%s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
415 cli_print(cli
, "\tIntercepted:\tno");
417 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
419 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
420 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
421 t
? "YES" : "no", t
? " (" : "",
422 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
424 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
428 b_in
= session
[s
].tbf_in
;
429 b_out
= session
[s
].tbf_out
;
431 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
432 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
433 "ByteSent", "PackSent", "PackDrop", "PackDelay");
436 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
438 (filter_list
[b_in
].next
? "*" : " "),
439 (b_in
< 100 ? "\t" : ""),
440 filter_list
[b_in
].rate
* 8,
441 filter_list
[b_in
].credit
,
442 filter_list
[b_in
].queued
,
443 filter_list
[b_in
].b_queued
,
444 filter_list
[b_in
].p_queued
,
445 filter_list
[b_in
].b_sent
,
446 filter_list
[b_in
].p_sent
,
447 filter_list
[b_in
].p_dropped
,
448 filter_list
[b_in
].p_delayed
);
451 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
453 (filter_list
[b_out
].next
? "*" : " "),
454 (b_out
< 100 ? "\t" : ""),
455 filter_list
[b_out
].rate
* 8,
456 filter_list
[b_out
].credit
,
457 filter_list
[b_out
].queued
,
458 filter_list
[b_out
].b_queued
,
459 filter_list
[b_out
].p_queued
,
460 filter_list
[b_out
].b_sent
,
461 filter_list
[b_out
].p_sent
,
462 filter_list
[b_out
].p_dropped
,
463 filter_list
[b_out
].p_delayed
);
470 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
485 for (i
= 1; i
< MAXSESSION
; i
++)
487 char *userip
, *tunnelip
;
488 if (!session
[i
].opened
) continue;
489 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
490 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
491 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
494 session
[i
].user
[0] ? session
[i
].user
: "*",
496 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
497 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
498 (session
[i
].walled_garden
) ? "Y" : "N",
499 abs(time_now
- (unsigned long)session
[i
].opened
),
500 (unsigned long)session
[i
].total_cout
,
501 (unsigned long)session
[i
].total_cin
,
502 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
504 session
[i
].calling
[0] ? session
[i
].calling
: "*");
505 if (userip
) free(userip
);
506 if (tunnelip
) free(tunnelip
);
511 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
513 int i
, x
, show_all
= 0;
521 if (CLI_HELP_REQUESTED
)
524 return cli_arg_help(cli
, 1,
525 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
528 return cli_arg_help(cli
, 1,
529 "all", "Show all tunnels, including unused",
530 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
537 if (strcmp(argv
[0], "all") == 0)
543 // Show individual tunnel
544 for (i
= 0; i
< argc
; i
++)
549 if (t
<= 0 || t
>= MAXTUNNEL
)
551 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
554 cli_print(cli
, "\r\nTunnel %d:", t
);
555 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
556 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
557 cli_print(cli
, "\tRemote IP:\t%s", inet_toa(htonl(tunnel
[t
].ip
)));
558 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
559 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
560 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
561 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
562 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
563 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
565 for (x
= 0; x
< MAXSESSION
; x
++)
566 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
567 sprintf(s
, "%s%u ", s
, x
);
569 cli_print(cli
, "\tSessions:\t%s", s
);
575 // Show tunnel summary
576 cli_print(cli
, "%4s %20s %20s %6s %s",
583 for (i
= 1; i
< MAXTUNNEL
; i
++)
586 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
588 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
589 cli_print(cli
, "%4d %20s %20s %6s %6d",
591 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
592 inet_toa(htonl(tunnel
[i
].ip
)),
593 states
[tunnel
[i
].state
],
600 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
607 if (CLI_HELP_REQUESTED
)
608 return cli_arg_help(cli
, 1,
609 "USER", "Show details for specific username",
612 for (i
= 0; i
< MAXSESSION
; i
++)
614 if (!session
[i
].opened
) continue;
615 if (!session
[i
].user
[0]) continue;
619 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
621 if (strcmp(argv
[j
], session
[i
].user
) == 0)
623 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
624 sargv
[sargc
] = sid
[sargc
];
632 cli_print(cli
, "%s", session
[i
].user
);
636 return cmd_show_session(cli
, "users", sargv
, sargc
);
641 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
643 if (CLI_HELP_REQUESTED
)
644 return CLI_HELP_NO_ARGS
;
646 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
647 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
648 GET_STAT(tun_rx_bytes
),
649 GET_STAT(tun_rx_packets
),
650 GET_STAT(tun_rx_errors
));
651 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
652 GET_STAT(tun_tx_bytes
),
653 GET_STAT(tun_tx_packets
),
654 GET_STAT(tun_tx_errors
));
657 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
658 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
659 GET_STAT(tunnel_rx_bytes
),
660 GET_STAT(tunnel_rx_packets
),
661 GET_STAT(tunnel_rx_errors
),
663 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
664 GET_STAT(tunnel_tx_bytes
),
665 GET_STAT(tunnel_tx_packets
),
666 GET_STAT(tunnel_tx_errors
),
667 GET_STAT(tunnel_retries
));
670 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
671 cli_print(cli
, "-----------------------------------------");
672 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
673 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
674 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
675 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
676 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
677 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
678 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
679 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
680 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
681 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
682 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
683 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
684 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
685 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
686 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
690 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
691 cli_print(cli
, "-----------------------------------------");
692 cli_print(cli
, "%-30s%lu", "call_processtun", GET_STAT(call_processtun
));
693 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
694 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
695 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
696 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
697 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
698 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
699 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
700 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
701 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
702 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
703 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
704 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
705 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
706 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
707 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
708 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
709 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
710 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
711 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
712 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
713 cli_print(cli
, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
714 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
715 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
716 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
717 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
722 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
728 if (CLI_HELP_REQUESTED
)
729 return cli_arg_help(cli
, 1,
730 "tag", "Include CVS release tag",
731 "file", "Include file versions",
734 for (i
= 0; i
< argc
; i
++)
735 if (!strcmp(argv
[i
], "tag"))
737 else if (!strcmp(argv
[i
], "file"))
740 cli_print(cli
, "L2TPNS %s", VERSION
);
743 char const *p
= strchr(cvs_name
, ':');
755 e
= strpbrk(p
, " \t$");
756 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
761 extern linked_list
*loaded_plugins
;
764 cli_print(cli
, "Files:");
765 cli_print(cli
, " %s", cvs_id_arp
);
767 cli_print(cli
, " %s", cvs_id_bgp
);
769 cli_print(cli
, " %s", cvs_id_cli
);
770 cli_print(cli
, " %s", cvs_id_cluster
);
771 cli_print(cli
, " %s", cvs_id_constants
);
772 cli_print(cli
, " %s", cvs_id_control
);
773 cli_print(cli
, " %s", cvs_id_icmp
);
774 cli_print(cli
, " %s", cvs_id_l2tpns
);
775 cli_print(cli
, " %s", cvs_id_ll
);
776 cli_print(cli
, " %s", cvs_id_md5
);
777 cli_print(cli
, " %s", cvs_id_ppp
);
778 cli_print(cli
, " %s", cvs_id_radius
);
779 cli_print(cli
, " %s", cvs_id_tbf
);
780 cli_print(cli
, " %s", cvs_id_util
);
782 ll_reset(loaded_plugins
);
783 while ((p
= ll_next(loaded_plugins
)))
785 char const **id
= dlsym(p
, "cvs_id");
787 cli_print(cli
, " %s", *id
);
794 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
797 int used
= 0, free
= 0, show_all
= 0;
799 if (!config
->cluster_iam_master
)
801 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
805 if (CLI_HELP_REQUESTED
)
808 return cli_arg_help(cli
, 1, NULL
);
810 return cli_arg_help(cli
, 1,
811 "all", "Show all pool addresses, including unused",
815 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
819 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
820 for (i
= 0; i
< MAXIPPOOL
; i
++)
822 if (!ip_address_pool
[i
].address
) continue;
823 if (ip_address_pool
[i
].assigned
)
825 cli_print(cli
, "%-15s\tY %8d %s",
826 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
832 if (ip_address_pool
[i
].last
)
833 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
834 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
835 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
837 cli_print(cli
, "%-15s\tN", inet_toa(htonl(ip_address_pool
[i
].address
)));
844 cli_print(cli
, "(Not displaying unused addresses)");
846 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
850 static FILE *save_config_fh
= 0;
851 static void print_save_config(struct cli_def
*cli
, char *string
)
854 fprintf(save_config_fh
, "%s\n", string
);
857 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
859 if (CLI_HELP_REQUESTED
)
860 return CLI_HELP_NO_ARGS
;
862 if ((save_config_fh
= fopen(config
->config_file
, "w")))
864 cli_print(cli
, "Writing configuration");
865 cli_print_callback(cli
, print_save_config
);
866 cmd_show_run(cli
, command
, argv
, argc
);
867 cli_print_callback(cli
, NULL
);
868 fclose(save_config_fh
);
873 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
878 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
880 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
884 if (CLI_HELP_REQUESTED
)
885 return CLI_HELP_NO_ARGS
;
887 cli_print(cli
, "# Current configuration:");
889 for (i
= 0; config_values
[i
].key
; i
++)
891 void *value
= ((void *)config
) + config_values
[i
].offset
;
892 if (config_values
[i
].type
== STRING
)
893 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
894 else if (config_values
[i
].type
== IP
)
895 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
896 else if (config_values
[i
].type
== SHORT
)
897 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
898 else if (config_values
[i
].type
== BOOL
)
899 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
900 else if (config_values
[i
].type
== INT
)
901 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
902 else if (config_values
[i
].type
== UNSIGNED_LONG
)
903 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
904 else if (config_values
[i
].type
== MAC
)
905 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
906 *(unsigned short *)(value
+ 0),
907 *(unsigned short *)(value
+ 1),
908 *(unsigned short *)(value
+ 2),
909 *(unsigned short *)(value
+ 3),
910 *(unsigned short *)(value
+ 4),
911 *(unsigned short *)(value
+ 5));
914 cli_print(cli
, "# Plugins");
915 for (i
= 0; i
< MAXPLUGINS
; i
++)
917 if (*config
->plugins
[i
])
919 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
924 if (config
->as_number
)
929 cli_print(cli
, "# BGP");
930 cli_print(cli
, "router bgp %u", config
->as_number
);
931 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
933 if (!config
->neighbour
[i
].name
[0])
936 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
938 k
= config
->neighbour
[i
].keepalive
;
939 h
= config
->neighbour
[i
].hold
;
946 k
= BGP_KEEPALIVE_TIME
;
952 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
957 cli_print(cli
, "# Filters");
958 for (i
= 0; i
< MAXFILTER
; i
++)
960 ip_filter_rulet
*rules
;
961 if (!*ip_filters
[i
].name
)
964 cli_print(cli
, "ip access-list %s %s",
965 ip_filters
[i
].extended
? "extended" : "standard",
968 rules
= ip_filters
[i
].rules
;
969 while (rules
->action
)
970 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
973 cli_print(cli
, "# end");
977 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
979 int i
, free
= 0, used
= 0, show_all
= 0;
990 if (CLI_HELP_REQUESTED
)
993 return cli_arg_help(cli
, 1, NULL
);
995 return cli_arg_help(cli
, 1,
996 "all", "Show all RADIUS sessions, including unused",
1000 cli_print(cli
, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1004 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1007 for (i
= 1; i
< MAXRADIUS
; i
++)
1009 if (radius
[i
].state
== RADIUSNULL
)
1014 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1016 cli_print(cli
, "%6d%6d%5d%6s%9d%9u%4d",
1020 states
[radius
[i
].state
],
1026 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1031 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1035 if (CLI_HELP_REQUESTED
)
1036 return CLI_HELP_NO_ARGS
;
1038 cli_print(cli
, "Plugins currently loaded:");
1039 for (i
= 0; i
< MAXPLUGINS
; i
++)
1040 if (*config
->plugins
[i
])
1041 cli_print(cli
, " %s", config
->plugins
[i
]);
1046 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1050 if (CLI_HELP_REQUESTED
)
1051 return CLI_HELP_NO_ARGS
;
1053 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1062 for (i
= 0; i
< MAXSESSION
; i
++)
1064 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1065 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1069 session
[i
].throttle_in
,
1070 session
[i
].throttle_out
,
1072 session
[i
].tbf_out
);
1078 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1080 if (CLI_HELP_REQUESTED
)
1081 return CLI_HELP_NO_ARGS
;
1083 cli_print(cli
, " _\n"
1089 " ( \\ `. `-. _,.-:\\\n"
1090 " \\ \\ `. `-._ __..--' ,-';/\n"
1091 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1092 " `. `. `-._ __..--' ,' /\n"
1093 " `. `-_ ``--..'' _.-' ,'\n"
1094 " `-_ `-.___ __,--' ,'\n"
1095 " `-.__ `----\"\"\" __.-'\n"
1096 "hh `--..____..--'");
1101 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1103 if (CLI_HELP_REQUESTED
)
1104 return CLI_HELP_NO_ARGS
;
1106 cli_print(cli
, "Counters cleared");
1107 SET_STAT(last_reset
, time(NULL
));
1111 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1116 if (CLI_HELP_REQUESTED
)
1117 return cli_arg_help(cli
, argc
> 1,
1118 "USER", "Username of session to drop", NULL
);
1120 if (!config
->cluster_iam_master
)
1122 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1128 cli_print(cli
, "Specify a user to drop");
1132 for (i
= 0; i
< argc
; i
++)
1134 if (!(s
= sessionbyuser(argv
[i
])))
1136 cli_print(cli
, "User %s is not connected", argv
[i
]);
1140 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1142 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1143 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1150 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1155 if (CLI_HELP_REQUESTED
)
1156 return cli_arg_help(cli
, argc
> 1,
1157 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1159 if (!config
->cluster_iam_master
)
1161 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1167 cli_print(cli
, "Specify a tunnel to drop");
1171 for (i
= 0; i
< argc
; i
++)
1173 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1175 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1181 cli_print(cli
, "Tunnel %d is not connected", t
);
1187 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1191 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1192 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1198 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1203 if (CLI_HELP_REQUESTED
)
1204 return cli_arg_help(cli
, argc
> 1,
1205 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1207 if (!config
->cluster_iam_master
)
1209 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1215 cli_print(cli
, "Specify a session id to drop");
1219 for (i
= 0; i
< argc
; i
++)
1221 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1223 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1227 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1229 cli_print(cli
, "Dropping session %d", s
);
1230 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1234 cli_print(cli
, "Session %d is not active.", s
);
1241 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1247 if (CLI_HELP_REQUESTED
)
1252 return cli_arg_help(cli
, 0,
1253 "USER", "Username of session to snoop", NULL
);
1256 return cli_arg_help(cli
, 0,
1257 "A.B.C.D", "IP address of snoop destination", NULL
);
1260 return cli_arg_help(cli
, 0,
1261 "N", "Port of snoop destination", NULL
);
1265 return cli_arg_help(cli
, 1, NULL
);
1272 if (!config
->cluster_iam_master
)
1274 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1280 cli_print(cli
, "Specify username, ip and port");
1284 if (!(s
= sessionbyuser(argv
[0])))
1286 cli_print(cli
, "User %s is not connected", argv
[0]);
1290 ip
= inet_addr(argv
[1]);
1291 if (!ip
|| ip
== INADDR_NONE
)
1293 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1297 port
= atoi(argv
[2]);
1300 cli_print(cli
, "Invalid port %s", argv
[2]);
1304 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(ip
), port
);
1305 cli_session_actions
[s
].snoop_ip
= ip
;
1306 cli_session_actions
[s
].snoop_port
= port
;
1307 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1312 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1317 if (CLI_HELP_REQUESTED
)
1318 return cli_arg_help(cli
, argc
> 1,
1319 "USER", "Username of session to unsnoop", NULL
);
1321 if (!config
->cluster_iam_master
)
1323 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1329 cli_print(cli
, "Specify a user to unsnoop");
1333 for (i
= 0; i
< argc
; i
++)
1335 if (!(s
= sessionbyuser(argv
[i
])))
1337 cli_print(cli
, "User %s is not connected", argv
[i
]);
1341 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1342 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1348 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1355 throttle USER - throttle in/out to default rate
1356 throttle USER RATE - throttle in/out to default rate
1357 throttle USER in RATE - throttle input only
1358 throttle USER out RATE - throttle output only
1359 throttle USER in RATE out RATE - throttle both
1362 if (CLI_HELP_REQUESTED
)
1367 return cli_arg_help(cli
, 0,
1368 "USER", "Username of session to throttle", NULL
);
1371 return cli_arg_help(cli
, 1,
1372 "RATE", "Rate in kbps (in and out)",
1373 "in", "Select incoming rate",
1374 "out", "Select outgoing rate", NULL
);
1377 return cli_arg_help(cli
, 1,
1378 "in", "Select incoming rate",
1379 "out", "Select outgoing rate", NULL
);
1382 if (isdigit(argv
[1][0]))
1383 return cli_arg_help(cli
, 1, NULL
);
1386 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1389 return cli_arg_help(cli
, argc
> 1, NULL
);
1393 if (!config
->cluster_iam_master
)
1395 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1401 cli_print(cli
, "Specify a user to throttle");
1405 if (!(s
= sessionbyuser(argv
[0])))
1407 cli_print(cli
, "User %s is not connected", argv
[0]);
1413 rate_in
= rate_out
= config
->rl_rate
;
1417 rate_in
= rate_out
= atoi(argv
[1]);
1420 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1424 else if (argc
== 3 || argc
== 5)
1427 for (i
= 1; i
< argc
- 1; i
+= 2)
1429 int len
= strlen(argv
[i
]);
1431 if (!strncasecmp(argv
[i
], "in", len
))
1432 r
= rate_in
= atoi(argv
[i
+1]);
1433 else if (!strncasecmp(argv
[i
], "out", len
))
1434 r
= rate_out
= atoi(argv
[i
+1]);
1438 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1445 cli_print(cli
, "Invalid arguments");
1449 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1451 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1455 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1456 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1457 cli_session_actions
[s
].throttle_in
= rate_in
;
1459 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1460 cli_session_actions
[s
].throttle_out
= rate_out
;
1462 if (cli_session_actions
[s
].throttle_in
== -1 &&
1463 cli_session_actions
[s
].throttle_out
== -1)
1465 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1469 cli_print(cli
, "Throttling user %s", argv
[0]);
1470 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1475 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1480 if (CLI_HELP_REQUESTED
)
1481 return cli_arg_help(cli
, argc
> 1,
1482 "USER", "Username of session to unthrottle", NULL
);
1484 if (!config
->cluster_iam_master
)
1486 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1492 cli_print(cli
, "Specify a user to unthrottle");
1496 for (i
= 0; i
< argc
; i
++)
1498 if (!(s
= sessionbyuser(argv
[i
])))
1500 cli_print(cli
, "User %s is not connected", argv
[i
]);
1504 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1506 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1507 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1511 cli_print(cli
, "User %s not throttled", argv
[i
]);
1518 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1522 if (CLI_HELP_REQUESTED
)
1523 return cli_arg_help(cli
, 1,
1524 "all", "Enable debugging for all except \"data\"",
1525 "critical", "", // FIXME: add descriptions
1535 char *p
= (char *) &debug_flags
;
1536 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1540 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1541 (debug_flags
.critical
) ? " critical" : "",
1542 (debug_flags
.error
) ? " error" : "",
1543 (debug_flags
.warning
) ? " warning" : "",
1544 (debug_flags
.info
) ? " info" : "",
1545 (debug_flags
.calls
) ? " calls" : "",
1546 (debug_flags
.data
) ? " data" : "");
1552 cli_print(cli
, "Debugging off");
1556 for (i
= 0; i
< argc
; i
++)
1558 int len
= strlen(argv
[i
]);
1560 if (argv
[i
][0] == 'c' && len
< 2)
1561 len
= 2; /* distinguish [cr]itical from [ca]lls */
1563 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 1; continue; }
1564 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 1; continue; }
1565 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 1; continue; }
1566 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 1; continue; }
1567 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 1; continue; }
1568 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 1; continue; }
1569 if (!strncasecmp(argv
[i
], "all", len
))
1571 memset(&debug_flags
, 1, sizeof(debug_flags
));
1572 debug_flags
.data
= 0;
1576 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1582 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1586 if (CLI_HELP_REQUESTED
)
1587 return cli_arg_help(cli
, 1,
1588 "all", "Disable all debugging",
1589 "critical", "", // FIXME: add descriptions
1599 memset(&debug_flags
, 0, sizeof(debug_flags
));
1603 for (i
= 0; i
< argc
; i
++)
1605 int len
= strlen(argv
[i
]);
1607 if (argv
[i
][0] == 'c' && len
< 2)
1608 len
= 2; /* distinguish [cr]itical from [ca]lls */
1610 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 0; continue; }
1611 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 0; continue; }
1612 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 0; continue; }
1613 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 0; continue; }
1614 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 0; continue; }
1615 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 0; continue; }
1616 if (!strncasecmp(argv
[i
], "all", len
))
1618 memset(&debug_flags
, 0, sizeof(debug_flags
));
1622 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1628 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1630 int i
, firstfree
= 0;
1632 if (CLI_HELP_REQUESTED
)
1633 return cli_arg_help(cli
, argc
> 1,
1634 "PLUGIN", "Name of plugin to load", NULL
);
1638 cli_print(cli
, "Specify a plugin to load");
1642 for (i
= 0; i
< MAXPLUGINS
; i
++)
1644 if (!*config
->plugins
[i
] && !firstfree
)
1646 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1648 cli_print(cli
, "Plugin is already loaded");
1655 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1656 config
->reload_config
= 1;
1657 cli_print(cli
, "Loading plugin %s", argv
[0]);
1663 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1667 if (CLI_HELP_REQUESTED
)
1668 return cli_arg_help(cli
, argc
> 1,
1669 "PLUGIN", "Name of plugin to unload", NULL
);
1673 cli_print(cli
, "Specify a plugin to remove");
1677 for (i
= 0; i
< MAXPLUGINS
; i
++)
1679 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1681 config
->reload_config
= 1;
1682 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1687 cli_print(cli
, "Plugin is not loaded");
1691 static char *duration(time_t secs
)
1693 static char *buf
= NULL
;
1696 if (!buf
) buf
= calloc(64, 1);
1700 int days
= secs
/ 86400;
1701 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1707 int mins
= secs
/ 60;
1708 int hrs
= mins
/ 60;
1711 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1713 else if (secs
>= 60)
1715 int mins
= secs
/ 60;
1716 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1719 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1724 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1727 char buf
[100], *p
= buf
, *loads
[3];
1728 int i
, num_sessions
= 0;
1730 if (CLI_HELP_REQUESTED
)
1731 return CLI_HELP_NO_ARGS
;
1733 fh
= fopen("/proc/loadavg", "r");
1734 fgets(buf
, 100, fh
);
1737 for (i
= 0; i
< 3; i
++)
1738 loads
[i
] = strdup(strsep(&p
, " "));
1741 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1743 for (i
= 1; i
< MAXSESSION
; i
++)
1744 if (session
[i
].opened
) num_sessions
++;
1746 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1748 duration(time_now
- config
->start_time
),
1750 loads
[0], loads
[1], loads
[2]
1752 for (i
= 0; i
< 3; i
++)
1753 if (loads
[i
]) free(loads
[i
]);
1755 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1760 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1764 if (CLI_HELP_REQUESTED
)
1770 int len
= strlen(argv
[0])-1;
1771 for (i
= 0; config_values
[i
].key
; i
++)
1772 if (!len
|| !strncmp(argv
[0], config_values
[i
].key
, len
))
1773 cli_print(cli
, " %s", config_values
[i
].key
);
1779 return cli_arg_help(cli
, 0,
1780 "VALUE", "Value for variable", NULL
);
1784 return cli_arg_help(cli
, 1, NULL
);
1793 cli_print(cli
, "Specify variable and value");
1797 for (i
= 0; config_values
[i
].key
; i
++)
1799 void *value
= ((void *)config
) + config_values
[i
].offset
;
1800 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1802 // Found a value to set
1803 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1804 switch (config_values
[i
].type
)
1807 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1810 *(int *)value
= atoi(argv
[1]);
1813 *(unsigned long *)value
= atol(argv
[1]);
1816 *(short *)value
= atoi(argv
[1]);
1819 *(unsigned *)value
= inet_addr(argv
[1]);
1822 parsemac(argv
[1], (char *)value
);
1825 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1831 cli_print(cli
, "Unknown variable type");
1834 config
->reload_config
= 1;
1839 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1843 int regular_stuff(struct cli_def
*cli
)
1845 int i
= debug_rb_tail
;
1849 while (i
!= ringbuffer
->tail
)
1851 int show_message
= 0;
1853 if (*ringbuffer
->buffer
[i
].message
)
1855 // Always show messages if we are doing general debug
1856 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1857 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1858 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1859 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1860 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1861 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1866 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1868 struct in_addr addr
;
1870 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1871 ipaddr
= inet_ntoa(addr
);
1873 cli_print(cli
, "\r%s-%s-%u-%u %s",
1874 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1876 ringbuffer
->buffer
[i
].tunnel
,
1877 ringbuffer
->buffer
[i
].session
,
1878 ringbuffer
->buffer
[i
].message
);
1883 if (++i
== ringbuffer
->tail
) break;
1884 if (i
== RINGBUFFER_SIZE
) i
= 0;
1887 debug_rb_tail
= ringbuffer
->tail
;
1895 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1899 if (CLI_HELP_REQUESTED
)
1900 return cli_arg_help(cli
, argc
> 1,
1901 "<1-65535>", "Autonomous system number", NULL
);
1903 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1905 cli_print(cli
, "Invalid autonomous system number");
1909 if (bgp_configured
&& as
!= config
->as_number
)
1911 cli_print(cli
, "Can't change local AS on a running system");
1915 config
->as_number
= as
;
1916 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1921 static int find_bgp_neighbour(char const *name
)
1926 in_addr_t addrs
[4] = { 0 };
1929 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1932 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1933 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1935 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1937 if (!config
->neighbour
[i
].name
[0])
1939 if (new == -1) new = i
;
1943 if (!strcmp(name
, config
->neighbour
[i
].name
))
1946 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1949 for (a
= h
->h_addr_list
; *a
; a
++)
1952 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1953 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1961 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1967 if (CLI_HELP_REQUESTED
)
1972 return cli_arg_help(cli
, 0,
1973 "A.B.C.D", "BGP neighbour address",
1974 "NAME", "BGP neighbour name",
1978 return cli_arg_help(cli
, 0,
1979 "remote-as", "Set remote autonomous system number",
1980 "timers", "Set timers",
1984 if (MATCH("remote-as", argv
[1]))
1985 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
1987 if (MATCH("timers", argv
[1]))
1990 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
1993 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
1995 if (argc
== 5 && !argv
[4][1])
1996 return cli_arg_help(cli
, 1, NULL
);
2005 cli_print(cli
, "Invalid arguments");
2009 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2011 cli_print(cli
, "Invalid neighbour");
2017 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2021 if (MATCH("remote-as", argv
[1]))
2023 int as
= atoi(argv
[2]);
2024 if (as
< 0 || as
> 65535)
2026 cli_print(cli
, "Invalid autonomous system number");
2030 if (!config
->neighbour
[i
].name
[0])
2032 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2033 config
->neighbour
[i
].keepalive
= -1;
2034 config
->neighbour
[i
].hold
= -1;
2037 config
->neighbour
[i
].as
= as
;
2041 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2043 cli_print(cli
, "Invalid arguments");
2047 if (!config
->neighbour
[i
].name
[0])
2049 cli_print(cli
, "Specify remote-as first");
2053 keepalive
= atoi(argv
[2]);
2054 hold
= atoi(argv
[3]);
2056 if (keepalive
< 1 || keepalive
> 65535)
2058 cli_print(cli
, "Invalid keepalive time");
2062 if (hold
< 3 || hold
> 65535)
2064 cli_print(cli
, "Invalid hold time");
2068 if (keepalive
== BGP_KEEPALIVE_TIME
)
2069 keepalive
= -1; // using default value
2071 if (hold
== BGP_HOLD_TIME
)
2074 config
->neighbour
[i
].keepalive
= keepalive
;
2075 config
->neighbour
[i
].hold
= hold
;
2080 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2084 if (CLI_HELP_REQUESTED
)
2085 return cli_arg_help(cli
, argc
> 0,
2086 "A.B.C.D", "BGP neighbour address",
2087 "NAME", "BGP neighbour name",
2092 cli_print(cli
, "Specify a BGP neighbour");
2096 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2098 cli_print(cli
, "Invalid neighbour");
2102 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2104 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2108 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2112 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2118 if (!bgp_configured
)
2121 if (CLI_HELP_REQUESTED
)
2122 return cli_arg_help(cli
, 1,
2123 "A.B.C.D", "BGP neighbour address",
2124 "NAME", "BGP neighbour name",
2127 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2128 BGP_VERSION
, inet_toa(my_address
), (int) config
->as_number
);
2132 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2134 if (!*bgp_peers
[i
].name
)
2137 addr
= inet_toa(bgp_peers
[i
].addr
);
2138 if (argc
&& strcmp(addr
, argv
[0]) &&
2139 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2145 cli_print(cli
, "Peer AS Address "
2146 "State Retries Retry in Route Pend Timers");
2147 cli_print(cli
, "------------------ ----- --------------- "
2148 "----------- ------- -------- ----- ---- ---------");
2151 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s %4d %4d",
2155 bgp_state_str(bgp_peers
[i
].state
),
2156 bgp_peers
[i
].retry_count
,
2157 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2158 bgp_peers
[i
].routing
? "yes" : "no",
2159 bgp_peers
[i
].update_routes
? "yes" : "no",
2160 bgp_peers
[i
].keepalive
,
2167 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2172 if (!bgp_configured
)
2175 if (CLI_HELP_REQUESTED
)
2176 return cli_arg_help(cli
, 1,
2177 "A.B.C.D", "BGP neighbour address",
2178 "NAME", "BGP neighbour name",
2181 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2183 if (bgp_peers
[i
].state
!= Established
)
2186 if (!bgp_peers
[i
].routing
)
2189 addr
= inet_toa(bgp_peers
[i
].addr
);
2190 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2193 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2194 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2200 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2205 if (!bgp_configured
)
2208 if (CLI_HELP_REQUESTED
)
2209 return cli_arg_help(cli
, 1,
2210 "A.B.C.D", "BGP neighbour address",
2211 "NAME", "BGP neighbour name",
2214 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2216 if (bgp_peers
[i
].state
!= Established
)
2219 if (bgp_peers
[i
].routing
)
2222 addr
= inet_toa(bgp_peers
[i
].addr
);
2223 if (argc
&& strcmp(addr
, argv
[0]) &&
2224 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2227 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2228 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2234 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2239 if (!bgp_configured
)
2242 if (CLI_HELP_REQUESTED
)
2243 return cli_arg_help(cli
, 1,
2244 "A.B.C.D", "BGP neighbour address",
2245 "NAME", "BGP neighbour name",
2248 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2250 if (!*bgp_peers
[i
].name
)
2253 addr
= inet_toa(bgp_peers
[i
].addr
);
2254 if (argc
&& strcmp(addr
, argv
[0]) &&
2255 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2258 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2259 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2267 static int find_access_list(char const *name
)
2271 for (i
= 0; i
< MAXFILTER
; i
++)
2272 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2278 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2282 if (CLI_HELP_REQUESTED
)
2287 return cli_arg_help(cli
, 0,
2288 "standard", "Standard syntax",
2289 "extended", "Extended syntax",
2293 return cli_arg_help(cli
, argv
[1][1],
2294 "NAME", "Access-list name",
2298 if (argc
== 3 && !argv
[2][1])
2299 return cli_arg_help(cli
, 1, NULL
);
2307 cli_print(cli
, "Specify access-list type and name");
2311 if (MATCH("standard", argv
[0]))
2313 else if (MATCH("extended", argv
[0]))
2317 cli_print(cli
, "Invalid access-list type");
2321 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2322 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv
[1]))
2324 cli_print(cli
, "Invalid access-list name");
2328 filt
= find_access_list(argv
[1]);
2333 cli_print(cli
, "Too many access-lists");
2338 if (!*ip_filters
[filt
].name
)
2340 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2341 strcpy(ip_filters
[filt
].name
, argv
[1]);
2342 ip_filters
[filt
].extended
= extended
;
2344 else if (ip_filters
[filt
].extended
!= extended
)
2346 cli_print(cli
, "Access-list is %s",
2347 ip_filters
[filt
].extended
? "extended" : "standard");
2352 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2356 if (filt
< 0 || !*ip_filters
[filt
].name
)
2358 cli_print(cli
, "Access-list not defined");
2363 if (ip_filters
[filt
].used
)
2365 cli_print(cli
, "Access-list in use");
2369 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2373 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2375 return access_list(cli
, argv
, argc
, 1);
2378 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2380 return access_list(cli
, argv
, argc
, 0);
2383 static int show_ip_wild(char *buf
, ipt ip
, ipt wild
)
2386 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2387 return sprintf(buf
, " any");
2389 if (wild
== INADDR_ANY
)
2390 return sprintf(buf
, " host %s", inet_toa(ip
));
2392 i
= sprintf(buf
, " %s", inet_toa(ip
));
2393 return i
+ sprintf(buf
+ i
, " %s", inet_toa(wild
));
2396 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2400 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2401 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2402 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2403 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2404 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2410 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2412 static char buf
[256];
2415 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2418 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2419 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2422 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2426 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2427 p
+= show_ports(p
, &rule
->src_ports
);
2429 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2430 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2431 p
+= show_ports(p
, &rule
->dst_ports
);
2433 if (rule
->proto
== IPPROTO_TCP
&& (rule
->tcp_sflags
|| rule
->tcp_cflags
))
2435 if (rule
->tcp_flag_op
== FILTER_FLAG_OP_ANY
&&
2436 rule
->tcp_sflags
== (TCP_FLAG_ACK
|TCP_FLAG_FIN
) &&
2437 rule
->tcp_cflags
== TCP_FLAG_SYN
)
2439 p
+= sprintf(p
, " established");
2443 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2444 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2445 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2446 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2447 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2448 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2449 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2450 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2451 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2452 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2453 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2454 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2455 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2462 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2464 static ip_filter_rulet rule
;
2465 struct in_addr addr
;
2469 if (CLI_HELP_REQUESTED
)
2473 cli_arg_help(cli
, 0,
2474 "ip", "Match IP packets",
2475 "tcp", "Match TCP packets",
2476 "udp", "Match UDP packets",
2482 // *sigh*, too darned complex
2483 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2489 cli_print(cli
, "Specify rule details");
2493 memset(&rule
, 0, sizeof(rule
));
2494 rule
.action
= (command
[0] == 'p')
2495 ? FILTER_ACTION_PERMIT
2496 : FILTER_ACTION_DENY
;
2498 if (MATCH("ip", argv
[0]))
2499 rule
.proto
= IPPROTO_IP
;
2500 else if (MATCH("udp", argv
[0]))
2501 rule
.proto
= IPPROTO_UDP
;
2502 else if (MATCH("tcp", argv
[0]))
2503 rule
.proto
= IPPROTO_TCP
;
2506 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2510 for (a
= 1, i
= 0; i
< 2; i
++)
2514 ip_filter_portt
*port
;
2519 wild
= &rule
.src_wild
;
2520 port
= &rule
.src_ports
;
2525 wild
= &rule
.dst_wild
;
2526 port
= &rule
.dst_ports
;
2529 cli_print(cli
, "Specify destination");
2534 if (MATCH("any", argv
[a
]))
2537 *wild
= INADDR_BROADCAST
;
2540 else if (MATCH("host", argv
[a
]))
2544 cli_print(cli
, "Specify host ip address");
2548 if (!inet_aton(argv
[a
], &addr
))
2550 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2562 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2566 if (!inet_aton(argv
[a
], &addr
))
2568 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2574 if (!inet_aton(argv
[++a
], &addr
))
2576 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2580 *wild
= addr
.s_addr
;
2584 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2588 if (MATCH("eq", argv
[a
]))
2589 port
->op
= FILTER_PORT_OP_EQ
;
2590 else if (MATCH("neq", argv
[a
]))
2591 port
->op
= FILTER_PORT_OP_NEQ
;
2592 else if (MATCH("gt", argv
[a
]))
2593 port
->op
= FILTER_PORT_OP_GT
;
2594 else if (MATCH("lt", argv
[a
]))
2595 port
->op
= FILTER_PORT_OP_LT
;
2596 else if (MATCH("range", argv
[a
]))
2597 port
->op
= FILTER_PORT_OP_RANGE
;
2604 cli_print(cli
, "Specify port");
2608 if (!(port
->port
= atoi(argv
[a
])))
2610 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2615 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2620 cli_print(cli
, "Specify port");
2624 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2626 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2633 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2635 if (MATCH("established", argv
[a
]))
2637 rule
.tcp_flag_op
= FILTER_FLAG_OP_ANY
;
2638 rule
.tcp_sflags
= (TCP_FLAG_ACK
|TCP_FLAG_FIN
);
2639 rule
.tcp_cflags
= TCP_FLAG_SYN
;
2642 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2643 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2645 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2646 ? FILTER_FLAG_OP_ANY
2647 : FILTER_FLAG_OP_ALL
;
2651 cli_print(cli
, "Specify tcp flags");
2655 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2659 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2661 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2662 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2663 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2664 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2665 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2666 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2669 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2680 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2687 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2689 static ip_filter_rulet rule
;
2690 struct in_addr addr
;
2692 if (CLI_HELP_REQUESTED
)
2696 cli_arg_help(cli
, argv
[0][1],
2697 "A.B.C.D", "Source address",
2698 "any", "Any source address",
2699 "host", "Source host",
2705 if (MATCH("any", argv
[0]))
2707 if (argc
== 2 && !argv
[1][1])
2708 cli_arg_help(cli
, 1, NULL
);
2710 else if (MATCH("host", argv
[0]))
2714 cli_arg_help(cli
, argv
[1][1],
2715 "A.B.C.D", "Host address",
2718 else if (argc
== 3 && !argv
[2][1])
2719 cli_arg_help(cli
, 1, NULL
);
2725 cli_arg_help(cli
, 1,
2726 "A.B.C.D", "Wildcard bits",
2729 else if (argc
== 3 && !argv
[2][1])
2730 cli_arg_help(cli
, 1, NULL
);
2738 cli_print(cli
, "Specify rule details");
2742 memset(&rule
, 0, sizeof(rule
));
2743 rule
.action
= (command
[0] == 'p')
2744 ? FILTER_ACTION_PERMIT
2745 : FILTER_ACTION_DENY
;
2747 rule
.proto
= IPPROTO_IP
;
2748 if (MATCH("any", argv
[0]))
2750 rule
.src_ip
= INADDR_ANY
;
2751 rule
.src_wild
= INADDR_BROADCAST
;
2753 else if (MATCH("host", argv
[0]))
2757 cli_print(cli
, "Specify host ip address");
2761 if (!inet_aton(argv
[1], &addr
))
2763 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2767 rule
.src_ip
= addr
.s_addr
;
2768 rule
.src_wild
= INADDR_ANY
;
2774 cli_print(cli
, "Specify source ip address and wildcard");
2778 if (!inet_aton(argv
[0], &addr
))
2780 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2784 rule
.src_ip
= addr
.s_addr
;
2788 if (!inet_aton(argv
[1], &addr
))
2790 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2794 rule
.src_wild
= addr
.s_addr
;
2797 rule
.src_wild
= INADDR_ANY
;
2803 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2806 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2807 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2808 : access_list_rule_std(cli
, command
, argv
, argc
);
2813 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2815 if (!ip_filters
[filt
].rules
[i
].action
)
2817 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2821 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2825 cli_print(cli
, "Too many rules");
2829 // Convert a string in the form of abcd.ef12.3456 into char[6]
2830 void parsemac(char *string
, char mac
[6])
2832 if (sscanf(string
, "%02x%02x.%02x%02x.%02x%02x", (unsigned int *)&mac
[0], (unsigned int *)&mac
[1], (unsigned int *)&mac
[2], (unsigned int *)&mac
[3], (unsigned int *)&mac
[4], (unsigned int *)&mac
[5]) == 6)
2834 if (sscanf(string
, "%02x%02x:%02x%02x:%02x%02x", (unsigned int *)&mac
[0], (unsigned int *)&mac
[1], (unsigned int *)&mac
[2], (unsigned int *)&mac
[3], (unsigned int *)&mac
[4], (unsigned int *)&mac
[5]) == 6)