1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.29 2004/11/27 05:19:53 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
].snoop_ip
&& session
[s
].snoop_port
)
409 cli_print(cli
, "\tIntercepted:\t%s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
411 cli_print(cli
, "\tIntercepted:\tno");
413 cli_print(cli
, "\tWalled Garden:\t%s", session
[s
].walled_garden
? "YES" : "no");
415 int t
= (session
[s
].throttle_in
|| session
[s
].throttle_out
);
416 cli_print(cli
, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
417 t
? "YES" : "no", t
? " (" : "",
418 session
[s
].throttle_in
, session
[s
].throttle_in
? "kbps" : t
? "-" : "",
420 session
[s
].throttle_out
, session
[s
].throttle_out
? "kbps" : t
? "-" : "",
424 b_in
= session
[s
].tbf_in
;
425 b_out
= session
[s
].tbf_out
;
427 cli_print(cli
, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
428 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
429 "ByteSent", "PackSent", "PackDrop", "PackDelay");
432 cli_print(cli
, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
434 (filter_list
[b_in
].next
? "*" : " "),
435 (b_in
< 100 ? "\t" : ""),
436 filter_list
[b_in
].rate
* 8,
437 filter_list
[b_in
].credit
,
438 filter_list
[b_in
].queued
,
439 filter_list
[b_in
].b_queued
,
440 filter_list
[b_in
].p_queued
,
441 filter_list
[b_in
].b_sent
,
442 filter_list
[b_in
].p_sent
,
443 filter_list
[b_in
].p_dropped
,
444 filter_list
[b_in
].p_delayed
);
447 cli_print(cli
, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
449 (filter_list
[b_out
].next
? "*" : " "),
450 (b_out
< 100 ? "\t" : ""),
451 filter_list
[b_out
].rate
* 8,
452 filter_list
[b_out
].credit
,
453 filter_list
[b_out
].queued
,
454 filter_list
[b_out
].b_queued
,
455 filter_list
[b_out
].p_queued
,
456 filter_list
[b_out
].b_sent
,
457 filter_list
[b_out
].p_sent
,
458 filter_list
[b_out
].p_dropped
,
459 filter_list
[b_out
].p_delayed
);
466 cli_print(cli
, "%5s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
481 for (i
= 1; i
< MAXSESSION
; i
++)
483 char *userip
, *tunnelip
;
484 if (!session
[i
].opened
) continue;
485 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
486 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
487 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
490 session
[i
].user
[0] ? session
[i
].user
: "*",
492 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
493 (session
[i
].throttle_in
|| session
[i
].throttle_out
) ? "Y" : "N",
494 (session
[i
].walled_garden
) ? "Y" : "N",
495 abs(time_now
- (unsigned long)session
[i
].opened
),
496 (unsigned long)session
[i
].total_cout
,
497 (unsigned long)session
[i
].total_cin
,
498 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
500 session
[i
].calling
[0] ? session
[i
].calling
: "*");
501 if (userip
) free(userip
);
502 if (tunnelip
) free(tunnelip
);
507 static int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
509 int i
, x
, show_all
= 0;
517 if (CLI_HELP_REQUESTED
)
520 return cli_arg_help(cli
, 1,
521 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
524 return cli_arg_help(cli
, 1,
525 "all", "Show all tunnels, including unused",
526 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
533 if (strcmp(argv
[0], "all") == 0)
539 // Show individual tunnel
540 for (i
= 0; i
< argc
; i
++)
545 if (t
<= 0 || t
>= MAXTUNNEL
)
547 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
550 cli_print(cli
, "\r\nTunnel %d:", t
);
551 cli_print(cli
, "\tState:\t\t%s", states
[tunnel
[t
].state
]);
552 cli_print(cli
, "\tHostname:\t%s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
553 cli_print(cli
, "\tRemote IP:\t%s", inet_toa(htonl(tunnel
[t
].ip
)));
554 cli_print(cli
, "\tRemote Port:\t%d", tunnel
[t
].port
);
555 cli_print(cli
, "\tRx Window:\t%u", tunnel
[t
].window
);
556 cli_print(cli
, "\tNext Recv:\t%u", tunnel
[t
].nr
);
557 cli_print(cli
, "\tNext Send:\t%u", tunnel
[t
].ns
);
558 cli_print(cli
, "\tQueue Len:\t%u", tunnel
[t
].controlc
);
559 cli_print(cli
, "\tLast Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
561 for (x
= 0; x
< MAXSESSION
; x
++)
562 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
563 sprintf(s
, "%s%u ", s
, x
);
565 cli_print(cli
, "\tSessions:\t%s", s
);
571 // Show tunnel summary
572 cli_print(cli
, "%4s %20s %20s %6s %s",
579 for (i
= 1; i
< MAXTUNNEL
; i
++)
582 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
)) continue;
584 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
585 cli_print(cli
, "%4d %20s %20s %6s %6d",
587 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
588 inet_toa(htonl(tunnel
[i
].ip
)),
589 states
[tunnel
[i
].state
],
596 static int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
603 if (CLI_HELP_REQUESTED
)
604 return cli_arg_help(cli
, 1,
605 "USER", "Show details for specific username",
608 for (i
= 0; i
< MAXSESSION
; i
++)
610 if (!session
[i
].opened
) continue;
611 if (!session
[i
].user
[0]) continue;
615 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
617 if (strcmp(argv
[j
], session
[i
].user
) == 0)
619 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
620 sargv
[sargc
] = sid
[sargc
];
628 cli_print(cli
, "%s", session
[i
].user
);
632 return cmd_show_session(cli
, "users", sargv
, sargc
);
637 static int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
639 if (CLI_HELP_REQUESTED
)
640 return CLI_HELP_NO_ARGS
;
642 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
643 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
644 GET_STAT(tun_rx_bytes
),
645 GET_STAT(tun_rx_packets
),
646 GET_STAT(tun_rx_errors
));
647 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
648 GET_STAT(tun_tx_bytes
),
649 GET_STAT(tun_tx_packets
),
650 GET_STAT(tun_tx_errors
));
653 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
654 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
655 GET_STAT(tunnel_rx_bytes
),
656 GET_STAT(tunnel_rx_packets
),
657 GET_STAT(tunnel_rx_errors
),
659 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
660 GET_STAT(tunnel_tx_bytes
),
661 GET_STAT(tunnel_tx_packets
),
662 GET_STAT(tunnel_tx_errors
),
663 GET_STAT(tunnel_retries
));
666 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
667 cli_print(cli
, "-----------------------------------------");
668 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
669 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
670 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
671 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
672 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
673 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
674 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
675 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
676 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
677 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
678 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
679 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
680 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
681 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
682 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
686 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
687 cli_print(cli
, "-----------------------------------------");
688 cli_print(cli
, "%-30s%lu", "call_processtun", GET_STAT(call_processtun
));
689 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
690 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
691 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
692 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
693 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
694 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
695 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
696 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
697 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
698 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
699 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
700 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
701 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
702 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
703 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
704 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
705 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
706 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
707 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
708 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
709 cli_print(cli
, "%-30s%lu", "call_assign_ip_address", GET_STAT(call_assign_ip_address
));
710 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
711 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
712 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
713 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
718 static int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
724 if (CLI_HELP_REQUESTED
)
725 return cli_arg_help(cli
, 1,
726 "tag", "Include CVS release tag",
727 "file", "Include file versions",
730 for (i
= 0; i
< argc
; i
++)
731 if (!strcmp(argv
[i
], "tag"))
733 else if (!strcmp(argv
[i
], "file"))
736 cli_print(cli
, "L2TPNS %s", VERSION
);
739 char const *p
= strchr(cvs_name
, ':');
751 e
= strpbrk(p
, " \t$");
752 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
757 extern linked_list
*loaded_plugins
;
760 cli_print(cli
, "Files:");
761 cli_print(cli
, " %s", cvs_id_arp
);
763 cli_print(cli
, " %s", cvs_id_bgp
);
765 cli_print(cli
, " %s", cvs_id_cli
);
766 cli_print(cli
, " %s", cvs_id_cluster
);
767 cli_print(cli
, " %s", cvs_id_constants
);
768 cli_print(cli
, " %s", cvs_id_control
);
769 cli_print(cli
, " %s", cvs_id_icmp
);
770 cli_print(cli
, " %s", cvs_id_l2tpns
);
771 cli_print(cli
, " %s", cvs_id_ll
);
772 cli_print(cli
, " %s", cvs_id_md5
);
773 cli_print(cli
, " %s", cvs_id_ppp
);
774 cli_print(cli
, " %s", cvs_id_radius
);
775 cli_print(cli
, " %s", cvs_id_tbf
);
776 cli_print(cli
, " %s", cvs_id_util
);
778 ll_reset(loaded_plugins
);
779 while ((p
= ll_next(loaded_plugins
)))
781 char const **id
= dlsym(p
, "cvs_id");
783 cli_print(cli
, " %s", *id
);
790 static int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
793 int used
= 0, free
= 0, show_all
= 0;
795 if (!config
->cluster_iam_master
)
797 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
801 if (CLI_HELP_REQUESTED
)
804 return cli_arg_help(cli
, 1, NULL
);
806 return cli_arg_help(cli
, 1,
807 "all", "Show all pool addresses, including unused",
811 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
815 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
816 for (i
= 0; i
< MAXIPPOOL
; i
++)
818 if (!ip_address_pool
[i
].address
) continue;
819 if (ip_address_pool
[i
].assigned
)
821 cli_print(cli
, "%-15s\tY %8d %s",
822 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
828 if (ip_address_pool
[i
].last
)
829 cli_print(cli
, "%-15s\tN %8s [%s] %ds",
830 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
831 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
833 cli_print(cli
, "%-15s\tN", inet_toa(htonl(ip_address_pool
[i
].address
)));
840 cli_print(cli
, "(Not displaying unused addresses)");
842 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
846 static FILE *save_config_fh
= 0;
847 static void print_save_config(struct cli_def
*cli
, char *string
)
850 fprintf(save_config_fh
, "%s\n", string
);
853 static int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
855 if (CLI_HELP_REQUESTED
)
856 return CLI_HELP_NO_ARGS
;
858 if ((save_config_fh
= fopen(config
->config_file
, "w")))
860 cli_print(cli
, "Writing configuration");
861 cli_print_callback(cli
, print_save_config
);
862 cmd_show_run(cli
, command
, argv
, argc
);
863 cli_print_callback(cli
, NULL
);
864 fclose(save_config_fh
);
869 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
874 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
);
876 static int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
880 if (CLI_HELP_REQUESTED
)
881 return CLI_HELP_NO_ARGS
;
883 cli_print(cli
, "# Current configuration:");
885 for (i
= 0; config_values
[i
].key
; i
++)
887 void *value
= ((void *)config
) + config_values
[i
].offset
;
888 if (config_values
[i
].type
== STRING
)
889 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
890 else if (config_values
[i
].type
== IP
)
891 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
892 else if (config_values
[i
].type
== SHORT
)
893 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
894 else if (config_values
[i
].type
== BOOL
)
895 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
896 else if (config_values
[i
].type
== INT
)
897 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
898 else if (config_values
[i
].type
== UNSIGNED_LONG
)
899 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
900 else if (config_values
[i
].type
== MAC
)
901 cli_print(cli
, "set %s %02x%02x.%02x%02x.%02x%02x", config_values
[i
].key
,
902 *(unsigned short *)(value
+ 0),
903 *(unsigned short *)(value
+ 1),
904 *(unsigned short *)(value
+ 2),
905 *(unsigned short *)(value
+ 3),
906 *(unsigned short *)(value
+ 4),
907 *(unsigned short *)(value
+ 5));
910 cli_print(cli
, "# Plugins");
911 for (i
= 0; i
< MAXPLUGINS
; i
++)
913 if (*config
->plugins
[i
])
915 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
920 if (config
->as_number
)
925 cli_print(cli
, "# BGP");
926 cli_print(cli
, "router bgp %u", config
->as_number
);
927 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
929 if (!config
->neighbour
[i
].name
[0])
932 cli_print(cli
, " neighbour %s remote-as %u", config
->neighbour
[i
].name
, config
->neighbour
[i
].as
);
934 k
= config
->neighbour
[i
].keepalive
;
935 h
= config
->neighbour
[i
].hold
;
942 k
= BGP_KEEPALIVE_TIME
;
948 cli_print(cli
, " neighbour %s timers %d %d", config
->neighbour
[i
].name
, k
, h
);
953 cli_print(cli
, "# Filters");
954 for (i
= 0; i
< MAXFILTER
; i
++)
956 ip_filter_rulet
*rules
;
957 if (!*ip_filters
[i
].name
)
960 cli_print(cli
, "ip access-list %s %s",
961 ip_filters
[i
].extended
? "extended" : "standard",
964 rules
= ip_filters
[i
].rules
;
965 while (rules
->action
)
966 cli_print(cli
, "%s", show_access_list_rule(ip_filters
[i
].extended
, rules
++));
969 cli_print(cli
, "# end");
973 static int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
975 int i
, free
= 0, used
= 0, show_all
= 0;
986 if (CLI_HELP_REQUESTED
)
989 return cli_arg_help(cli
, 1, NULL
);
991 return cli_arg_help(cli
, 1,
992 "all", "Show all RADIUS sessions, including unused",
996 cli_print(cli
, "%6s%6s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1000 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
1003 for (i
= 1; i
< MAXRADIUS
; i
++)
1005 if (radius
[i
].state
== RADIUSNULL
)
1010 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
1012 cli_print(cli
, "%6d%6d%5d%6s%9d%9u%4d",
1016 states
[radius
[i
].state
],
1022 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
1027 static int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1031 if (CLI_HELP_REQUESTED
)
1032 return CLI_HELP_NO_ARGS
;
1034 cli_print(cli
, "Plugins currently loaded:");
1035 for (i
= 0; i
< MAXPLUGINS
; i
++)
1036 if (*config
->plugins
[i
])
1037 cli_print(cli
, " %s", config
->plugins
[i
]);
1042 static int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1046 if (CLI_HELP_REQUESTED
)
1047 return CLI_HELP_NO_ARGS
;
1049 cli_print(cli
, "%5s %4s %-32s %7s %6s %6s %6s",
1058 for (i
= 0; i
< MAXSESSION
; i
++)
1060 if (session
[i
].throttle_in
|| session
[i
].throttle_out
)
1061 cli_print(cli
, "%5d %4d %-32s %6d %6d %6d %6d",
1065 session
[i
].throttle_in
,
1066 session
[i
].throttle_out
,
1068 session
[i
].tbf_out
);
1074 static int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1076 if (CLI_HELP_REQUESTED
)
1077 return CLI_HELP_NO_ARGS
;
1079 cli_print(cli
, " _\n"
1085 " ( \\ `. `-. _,.-:\\\n"
1086 " \\ \\ `. `-._ __..--' ,-';/\n"
1087 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1088 " `. `. `-._ __..--' ,' /\n"
1089 " `. `-_ ``--..'' _.-' ,'\n"
1090 " `-_ `-.___ __,--' ,'\n"
1091 " `-.__ `----\"\"\" __.-'\n"
1092 "hh `--..____..--'");
1097 static int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1099 if (CLI_HELP_REQUESTED
)
1100 return CLI_HELP_NO_ARGS
;
1102 cli_print(cli
, "Counters cleared");
1103 SET_STAT(last_reset
, time(NULL
));
1107 static int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1112 if (CLI_HELP_REQUESTED
)
1113 return cli_arg_help(cli
, argc
> 1,
1114 "USER", "Username of session to drop", NULL
);
1116 if (!config
->cluster_iam_master
)
1118 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1124 cli_print(cli
, "Specify a user to drop");
1128 for (i
= 0; i
< argc
; i
++)
1130 if (!(s
= sessionbyuser(argv
[i
])))
1132 cli_print(cli
, "User %s is not connected", argv
[i
]);
1136 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1138 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1139 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1146 static int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1151 if (CLI_HELP_REQUESTED
)
1152 return cli_arg_help(cli
, argc
> 1,
1153 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1155 if (!config
->cluster_iam_master
)
1157 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1163 cli_print(cli
, "Specify a tunnel to drop");
1167 for (i
= 0; i
< argc
; i
++)
1169 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1171 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1177 cli_print(cli
, "Tunnel %d is not connected", t
);
1183 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1187 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1188 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1194 static int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1199 if (CLI_HELP_REQUESTED
)
1200 return cli_arg_help(cli
, argc
> 1,
1201 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1203 if (!config
->cluster_iam_master
)
1205 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1211 cli_print(cli
, "Specify a session id to drop");
1215 for (i
= 0; i
< argc
; i
++)
1217 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1219 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1223 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1225 cli_print(cli
, "Dropping session %d", s
);
1226 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1230 cli_print(cli
, "Session %d is not active.", s
);
1237 static int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1243 if (CLI_HELP_REQUESTED
)
1248 return cli_arg_help(cli
, 0,
1249 "USER", "Username of session to snoop", NULL
);
1252 return cli_arg_help(cli
, 0,
1253 "A.B.C.D", "IP address of snoop destination", NULL
);
1256 return cli_arg_help(cli
, 0,
1257 "N", "Port of snoop destination", NULL
);
1261 return cli_arg_help(cli
, 1, NULL
);
1268 if (!config
->cluster_iam_master
)
1270 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1276 cli_print(cli
, "Specify username, ip and port");
1280 if (!(s
= sessionbyuser(argv
[0])))
1282 cli_print(cli
, "User %s is not connected", argv
[0]);
1286 ip
= inet_addr(argv
[1]);
1287 if (!ip
|| ip
== INADDR_NONE
)
1289 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1293 port
= atoi(argv
[2]);
1296 cli_print(cli
, "Invalid port %s", argv
[2]);
1300 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(ip
), port
);
1301 cli_session_actions
[s
].snoop_ip
= ip
;
1302 cli_session_actions
[s
].snoop_port
= port
;
1303 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1308 static int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1313 if (CLI_HELP_REQUESTED
)
1314 return cli_arg_help(cli
, argc
> 1,
1315 "USER", "Username of session to unsnoop", NULL
);
1317 if (!config
->cluster_iam_master
)
1319 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1325 cli_print(cli
, "Specify a user to unsnoop");
1329 for (i
= 0; i
< argc
; i
++)
1331 if (!(s
= sessionbyuser(argv
[i
])))
1333 cli_print(cli
, "User %s is not connected", argv
[i
]);
1337 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1338 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1344 static int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1351 throttle USER - throttle in/out to default rate
1352 throttle USER RATE - throttle in/out to default rate
1353 throttle USER in RATE - throttle input only
1354 throttle USER out RATE - throttle output only
1355 throttle USER in RATE out RATE - throttle both
1358 if (CLI_HELP_REQUESTED
)
1363 return cli_arg_help(cli
, 0,
1364 "USER", "Username of session to throttle", NULL
);
1367 return cli_arg_help(cli
, 1,
1368 "RATE", "Rate in kbps (in and out)",
1369 "in", "Select incoming rate",
1370 "out", "Select outgoing rate", NULL
);
1373 return cli_arg_help(cli
, 1,
1374 "in", "Select incoming rate",
1375 "out", "Select outgoing rate", NULL
);
1378 if (isdigit(argv
[1][0]))
1379 return cli_arg_help(cli
, 1, NULL
);
1382 return cli_arg_help(cli
, 0, "RATE", "Rate in kbps", NULL
);
1385 return cli_arg_help(cli
, argc
> 1, NULL
);
1389 if (!config
->cluster_iam_master
)
1391 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1397 cli_print(cli
, "Specify a user to throttle");
1401 if (!(s
= sessionbyuser(argv
[0])))
1403 cli_print(cli
, "User %s is not connected", argv
[0]);
1409 rate_in
= rate_out
= config
->rl_rate
;
1413 rate_in
= rate_out
= atoi(argv
[1]);
1416 cli_print(cli
, "Invalid rate \"%s\"", argv
[1]);
1420 else if (argc
== 3 || argc
== 5)
1423 for (i
= 1; i
< argc
- 1; i
+= 2)
1425 int len
= strlen(argv
[i
]);
1427 if (!strncasecmp(argv
[i
], "in", len
))
1428 r
= rate_in
= atoi(argv
[i
+1]);
1429 else if (!strncasecmp(argv
[i
], "out", len
))
1430 r
= rate_out
= atoi(argv
[i
+1]);
1434 cli_print(cli
, "Invalid rate specification \"%s %s\"", argv
[i
], argv
[i
+1]);
1441 cli_print(cli
, "Invalid arguments");
1445 if ((rate_in
&& session
[s
].throttle_in
) || (rate_out
&& session
[s
].throttle_out
))
1447 cli_print(cli
, "User %s already throttled, unthrottle first", argv
[0]);
1451 cli_session_actions
[s
].throttle_in
= cli_session_actions
[s
].throttle_out
= -1;
1452 if (rate_in
&& session
[s
].throttle_in
!= rate_in
)
1453 cli_session_actions
[s
].throttle_in
= rate_in
;
1455 if (rate_out
&& session
[s
].throttle_out
!= rate_out
)
1456 cli_session_actions
[s
].throttle_out
= rate_out
;
1458 if (cli_session_actions
[s
].throttle_in
== -1 &&
1459 cli_session_actions
[s
].throttle_out
== -1)
1461 cli_print(cli
, "User %s already throttled at this rate", argv
[0]);
1465 cli_print(cli
, "Throttling user %s", argv
[0]);
1466 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1471 static int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1476 if (CLI_HELP_REQUESTED
)
1477 return cli_arg_help(cli
, argc
> 1,
1478 "USER", "Username of session to unthrottle", NULL
);
1480 if (!config
->cluster_iam_master
)
1482 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1488 cli_print(cli
, "Specify a user to unthrottle");
1492 for (i
= 0; i
< argc
; i
++)
1494 if (!(s
= sessionbyuser(argv
[i
])))
1496 cli_print(cli
, "User %s is not connected", argv
[i
]);
1500 if (session
[s
].throttle_in
|| session
[s
].throttle_out
)
1502 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1503 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1507 cli_print(cli
, "User %s not throttled", argv
[i
]);
1514 static int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1518 if (CLI_HELP_REQUESTED
)
1519 return cli_arg_help(cli
, 1,
1520 "all", "Enable debugging for all except \"data\"",
1521 "critical", "", // FIXME: add descriptions
1531 char *p
= (char *) &debug_flags
;
1532 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1536 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1537 (debug_flags
.critical
) ? " critical" : "",
1538 (debug_flags
.error
) ? " error" : "",
1539 (debug_flags
.warning
) ? " warning" : "",
1540 (debug_flags
.info
) ? " info" : "",
1541 (debug_flags
.calls
) ? " calls" : "",
1542 (debug_flags
.data
) ? " data" : "");
1548 cli_print(cli
, "Debugging off");
1552 for (i
= 0; i
< argc
; i
++)
1554 int len
= strlen(argv
[i
]);
1556 if (argv
[i
][0] == 'c' && len
< 2)
1557 len
= 2; /* distinguish [cr]itical from [ca]lls */
1559 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 1; continue; }
1560 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 1; continue; }
1561 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 1; continue; }
1562 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 1; continue; }
1563 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 1; continue; }
1564 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 1; continue; }
1565 if (!strncasecmp(argv
[i
], "all", len
))
1567 memset(&debug_flags
, 1, sizeof(debug_flags
));
1568 debug_flags
.data
= 0;
1572 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1578 static int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1582 if (CLI_HELP_REQUESTED
)
1583 return cli_arg_help(cli
, 1,
1584 "all", "Disable all debugging",
1585 "critical", "", // FIXME: add descriptions
1595 memset(&debug_flags
, 0, sizeof(debug_flags
));
1599 for (i
= 0; i
< argc
; i
++)
1601 int len
= strlen(argv
[i
]);
1603 if (argv
[i
][0] == 'c' && len
< 2)
1604 len
= 2; /* distinguish [cr]itical from [ca]lls */
1606 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 0; continue; }
1607 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 0; continue; }
1608 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 0; continue; }
1609 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 0; continue; }
1610 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 0; continue; }
1611 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 0; continue; }
1612 if (!strncasecmp(argv
[i
], "all", len
))
1614 memset(&debug_flags
, 0, sizeof(debug_flags
));
1618 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1624 static int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1626 int i
, firstfree
= 0;
1628 if (CLI_HELP_REQUESTED
)
1629 return cli_arg_help(cli
, argc
> 1,
1630 "PLUGIN", "Name of plugin to load", NULL
);
1634 cli_print(cli
, "Specify a plugin to load");
1638 for (i
= 0; i
< MAXPLUGINS
; i
++)
1640 if (!*config
->plugins
[i
] && !firstfree
)
1642 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1644 cli_print(cli
, "Plugin is already loaded");
1651 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1652 config
->reload_config
= 1;
1653 cli_print(cli
, "Loading plugin %s", argv
[0]);
1659 static int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1663 if (CLI_HELP_REQUESTED
)
1664 return cli_arg_help(cli
, argc
> 1,
1665 "PLUGIN", "Name of plugin to unload", NULL
);
1669 cli_print(cli
, "Specify a plugin to remove");
1673 for (i
= 0; i
< MAXPLUGINS
; i
++)
1675 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1677 config
->reload_config
= 1;
1678 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1683 cli_print(cli
, "Plugin is not loaded");
1687 static char *duration(time_t secs
)
1689 static char *buf
= NULL
;
1692 if (!buf
) buf
= calloc(64, 1);
1696 int days
= secs
/ 86400;
1697 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1703 int mins
= secs
/ 60;
1704 int hrs
= mins
/ 60;
1707 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1709 else if (secs
>= 60)
1711 int mins
= secs
/ 60;
1712 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1715 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1720 static int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1723 char buf
[100], *p
= buf
, *loads
[3];
1724 int i
, num_sessions
= 0;
1726 if (CLI_HELP_REQUESTED
)
1727 return CLI_HELP_NO_ARGS
;
1729 fh
= fopen("/proc/loadavg", "r");
1730 fgets(buf
, 100, fh
);
1733 for (i
= 0; i
< 3; i
++)
1734 loads
[i
] = strdup(strsep(&p
, " "));
1737 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1739 for (i
= 1; i
< MAXSESSION
; i
++)
1740 if (session
[i
].opened
) num_sessions
++;
1742 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1744 duration(time_now
- config
->start_time
),
1746 loads
[0], loads
[1], loads
[2]
1748 for (i
= 0; i
< 3; i
++)
1749 if (loads
[i
]) free(loads
[i
]);
1751 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1756 static int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1760 if (CLI_HELP_REQUESTED
)
1766 int len
= strlen(argv
[0])-1;
1767 for (i
= 0; config_values
[i
].key
; i
++)
1768 if (!len
|| !strncmp(argv
[0], config_values
[i
].key
, len
))
1769 cli_print(cli
, " %s", config_values
[i
].key
);
1775 return cli_arg_help(cli
, 0,
1776 "VALUE", "Value for variable", NULL
);
1780 return cli_arg_help(cli
, 1, NULL
);
1789 cli_print(cli
, "Specify variable and value");
1793 for (i
= 0; config_values
[i
].key
; i
++)
1795 void *value
= ((void *)config
) + config_values
[i
].offset
;
1796 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1798 // Found a value to set
1799 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1800 switch (config_values
[i
].type
)
1803 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1806 *(int *)value
= atoi(argv
[1]);
1809 *(unsigned long *)value
= atol(argv
[1]);
1812 *(short *)value
= atoi(argv
[1]);
1815 *(unsigned *)value
= inet_addr(argv
[1]);
1818 parsemac(argv
[1], (char *)value
);
1821 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1827 cli_print(cli
, "Unknown variable type");
1830 config
->reload_config
= 1;
1835 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1839 int regular_stuff(struct cli_def
*cli
)
1841 int i
= debug_rb_tail
;
1845 while (i
!= ringbuffer
->tail
)
1847 int show_message
= 0;
1849 if (*ringbuffer
->buffer
[i
].message
)
1851 // Always show messages if we are doing general debug
1852 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1853 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1854 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1855 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1856 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1857 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1862 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1864 struct in_addr addr
;
1866 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1867 ipaddr
= inet_ntoa(addr
);
1869 cli_print(cli
, "\r%s-%s-%u-%u %s",
1870 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1872 ringbuffer
->buffer
[i
].tunnel
,
1873 ringbuffer
->buffer
[i
].session
,
1874 ringbuffer
->buffer
[i
].message
);
1879 if (++i
== ringbuffer
->tail
) break;
1880 if (i
== RINGBUFFER_SIZE
) i
= 0;
1883 debug_rb_tail
= ringbuffer
->tail
;
1891 static int cmd_router_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1895 if (CLI_HELP_REQUESTED
)
1896 return cli_arg_help(cli
, argc
> 1,
1897 "<1-65535>", "Autonomous system number", NULL
);
1899 if (argc
!= 1 || (as
= atoi(argv
[0])) < 1 || as
> 65535)
1901 cli_print(cli
, "Invalid autonomous system number");
1905 if (bgp_configured
&& as
!= config
->as_number
)
1907 cli_print(cli
, "Can't change local AS on a running system");
1911 config
->as_number
= as
;
1912 cli_set_configmode(cli
, MODE_CONFIG_BGP
, "router");
1917 static int find_bgp_neighbour(char const *name
)
1922 in_addr_t addrs
[4] = { 0 };
1925 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
1928 for (i
= 0; i
< sizeof(addrs
) / sizeof(*addrs
) && h
->h_addr_list
[i
]; i
++)
1929 memcpy(&addrs
[i
], h
->h_addr_list
[i
], sizeof(*addrs
));
1931 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1933 if (!config
->neighbour
[i
].name
[0])
1935 if (new == -1) new = i
;
1939 if (!strcmp(name
, config
->neighbour
[i
].name
))
1942 if (!(h
= gethostbyname(config
->neighbour
[i
].name
)) || h
->h_addrtype
!= AF_INET
)
1945 for (a
= h
->h_addr_list
; *a
; a
++)
1948 for (j
= 0; j
< sizeof(addrs
) / sizeof(*addrs
) && addrs
[j
]; j
++)
1949 if (!memcmp(&addrs
[j
], *a
, sizeof(*addrs
)))
1957 static int cmd_router_bgp_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1963 if (CLI_HELP_REQUESTED
)
1968 return cli_arg_help(cli
, 0,
1969 "A.B.C.D", "BGP neighbour address",
1970 "NAME", "BGP neighbour name",
1974 return cli_arg_help(cli
, 0,
1975 "remote-as", "Set remote autonomous system number",
1976 "timers", "Set timers",
1980 if (MATCH("remote-as", argv
[1]))
1981 return cli_arg_help(cli
, argv
[2][1], "<1-65535>", "Autonomous system number", NULL
);
1983 if (MATCH("timers", argv
[1]))
1986 return cli_arg_help(cli
, 0, "<1-65535>", "Keepalive time", NULL
);
1989 return cli_arg_help(cli
, argv
[3][1], "<3-65535>", "Hold time", NULL
);
1991 if (argc
== 5 && !argv
[4][1])
1992 return cli_arg_help(cli
, 1, NULL
);
2001 cli_print(cli
, "Invalid arguments");
2005 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2007 cli_print(cli
, "Invalid neighbour");
2013 cli_print(cli
, "Too many neighbours (max %d)", BGP_NUM_PEERS
);
2017 if (MATCH("remote-as", argv
[1]))
2019 int as
= atoi(argv
[2]);
2020 if (as
< 0 || as
> 65535)
2022 cli_print(cli
, "Invalid autonomous system number");
2026 if (!config
->neighbour
[i
].name
[0])
2028 snprintf(config
->neighbour
[i
].name
, sizeof(config
->neighbour
[i
].name
), argv
[0]);
2029 config
->neighbour
[i
].keepalive
= -1;
2030 config
->neighbour
[i
].hold
= -1;
2033 config
->neighbour
[i
].as
= as
;
2037 if (argc
!= 4 || !MATCH("timers", argv
[1]))
2039 cli_print(cli
, "Invalid arguments");
2043 if (!config
->neighbour
[i
].name
[0])
2045 cli_print(cli
, "Specify remote-as first");
2049 keepalive
= atoi(argv
[2]);
2050 hold
= atoi(argv
[3]);
2052 if (keepalive
< 1 || keepalive
> 65535)
2054 cli_print(cli
, "Invalid keepalive time");
2058 if (hold
< 3 || hold
> 65535)
2060 cli_print(cli
, "Invalid hold time");
2064 if (keepalive
== BGP_KEEPALIVE_TIME
)
2065 keepalive
= -1; // using default value
2067 if (hold
== BGP_HOLD_TIME
)
2070 config
->neighbour
[i
].keepalive
= keepalive
;
2071 config
->neighbour
[i
].hold
= hold
;
2076 static int cmd_router_bgp_no_neighbour(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2080 if (CLI_HELP_REQUESTED
)
2081 return cli_arg_help(cli
, argc
> 0,
2082 "A.B.C.D", "BGP neighbour address",
2083 "NAME", "BGP neighbour name",
2088 cli_print(cli
, "Specify a BGP neighbour");
2092 if ((i
= find_bgp_neighbour(argv
[0])) == -2)
2094 cli_print(cli
, "Invalid neighbour");
2098 if (i
< 0 || !config
->neighbour
[i
].name
[0])
2100 cli_print(cli
, "Neighbour %s not configured", argv
[0]);
2104 memset(&config
->neighbour
[i
], 0, sizeof(config
->neighbour
[i
]));
2108 static int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2114 if (!bgp_configured
)
2117 if (CLI_HELP_REQUESTED
)
2118 return cli_arg_help(cli
, 1,
2119 "A.B.C.D", "BGP neighbour address",
2120 "NAME", "BGP neighbour name",
2123 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d",
2124 BGP_VERSION
, inet_toa(my_address
), (int) config
->as_number
);
2128 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2130 if (!*bgp_peers
[i
].name
)
2133 addr
= inet_toa(bgp_peers
[i
].addr
);
2134 if (argc
&& strcmp(addr
, argv
[0]) &&
2135 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2141 cli_print(cli
, "Peer AS Address "
2142 "State Retries Retry in Route Pend Timers");
2143 cli_print(cli
, "------------------ ----- --------------- "
2144 "----------- ------- -------- ----- ---- ---------");
2147 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s %4d %4d",
2151 bgp_state_str(bgp_peers
[i
].state
),
2152 bgp_peers
[i
].retry_count
,
2153 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
2154 bgp_peers
[i
].routing
? "yes" : "no",
2155 bgp_peers
[i
].update_routes
? "yes" : "no",
2156 bgp_peers
[i
].keepalive
,
2163 static int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2168 if (!bgp_configured
)
2171 if (CLI_HELP_REQUESTED
)
2172 return cli_arg_help(cli
, 1,
2173 "A.B.C.D", "BGP neighbour address",
2174 "NAME", "BGP neighbour name",
2177 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2179 if (bgp_peers
[i
].state
!= Established
)
2182 if (!bgp_peers
[i
].routing
)
2185 addr
= inet_toa(bgp_peers
[i
].addr
);
2186 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
2189 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
2190 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
2196 static int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2201 if (!bgp_configured
)
2204 if (CLI_HELP_REQUESTED
)
2205 return cli_arg_help(cli
, 1,
2206 "A.B.C.D", "BGP neighbour address",
2207 "NAME", "BGP neighbour name",
2210 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2212 if (bgp_peers
[i
].state
!= Established
)
2215 if (bgp_peers
[i
].routing
)
2218 addr
= inet_toa(bgp_peers
[i
].addr
);
2219 if (argc
&& strcmp(addr
, argv
[0]) &&
2220 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2223 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
2224 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
2230 static int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2235 if (!bgp_configured
)
2238 if (CLI_HELP_REQUESTED
)
2239 return cli_arg_help(cli
, 1,
2240 "A.B.C.D", "BGP neighbour address",
2241 "NAME", "BGP neighbour name",
2244 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
2246 if (!*bgp_peers
[i
].name
)
2249 addr
= inet_toa(bgp_peers
[i
].addr
);
2250 if (argc
&& strcmp(addr
, argv
[0]) &&
2251 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
2254 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
2255 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);
2263 static int find_access_list(char const *name
)
2267 for (i
= 0; i
< MAXFILTER
; i
++)
2268 if (!(*ip_filters
[i
].name
&& strcmp(ip_filters
[i
].name
, name
)))
2274 static int access_list(struct cli_def
*cli
, char **argv
, int argc
, int add
)
2278 if (CLI_HELP_REQUESTED
)
2283 return cli_arg_help(cli
, 0,
2284 "standard", "Standard syntax",
2285 "extended", "Extended syntax",
2289 return cli_arg_help(cli
, argv
[1][1],
2290 "NAME", "Access-list name",
2294 if (argc
== 3 && !argv
[2][1])
2295 return cli_arg_help(cli
, 1, NULL
);
2303 cli_print(cli
, "Specify access-list type and name");
2307 if (MATCH("standard", argv
[0]))
2309 else if (MATCH("extended", argv
[0]))
2313 cli_print(cli
, "Invalid access-list type");
2317 if (strlen(argv
[1]) > sizeof(ip_filters
[0].name
) - 1 ||
2318 strspn(argv
[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(argv
[1]))
2320 cli_print(cli
, "Invalid access-list name");
2324 filt
= find_access_list(argv
[1]);
2329 cli_print(cli
, "Too many access-lists");
2334 if (!*ip_filters
[filt
].name
)
2336 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2337 strcpy(ip_filters
[filt
].name
, argv
[1]);
2338 ip_filters
[filt
].extended
= extended
;
2340 else if (ip_filters
[filt
].extended
!= extended
)
2342 cli_print(cli
, "Access-list is %s",
2343 ip_filters
[filt
].extended
? "extended" : "standard");
2348 cli_set_configmode(cli
, MODE_CONFIG_NACL
, extended
? "ext-nacl" : "std-nacl");
2352 if (filt
< 0 || !*ip_filters
[filt
].name
)
2354 cli_print(cli
, "Access-list not defined");
2359 if (ip_filters
[filt
].used
)
2361 cli_print(cli
, "Access-list in use");
2365 memset(&ip_filters
[filt
], 0, sizeof(ip_filters
[filt
]));
2369 static int cmd_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2371 return access_list(cli
, argv
, argc
, 1);
2374 static int cmd_no_ip_access_list(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2376 return access_list(cli
, argv
, argc
, 0);
2379 static int show_ip_wild(char *buf
, ipt ip
, ipt wild
)
2382 if (ip
== INADDR_ANY
&& wild
== INADDR_BROADCAST
)
2383 return sprintf(buf
, " any");
2385 if (wild
== INADDR_ANY
)
2386 return sprintf(buf
, " host %s", inet_toa(ip
));
2388 i
= sprintf(buf
, " %s", inet_toa(ip
));
2389 return i
+ sprintf(buf
+ i
, " %s", inet_toa(wild
));
2392 static int show_ports(char *buf
, ip_filter_portt
*ports
)
2396 case FILTER_PORT_OP_EQ
: return sprintf(buf
, " eq %u", ports
->port
);
2397 case FILTER_PORT_OP_NEQ
: return sprintf(buf
, " neq %u", ports
->port
);
2398 case FILTER_PORT_OP_GT
: return sprintf(buf
, " gt %u", ports
->port
);
2399 case FILTER_PORT_OP_LT
: return sprintf(buf
, " lt %u", ports
->port
);
2400 case FILTER_PORT_OP_RANGE
: return sprintf(buf
, " range %u %u", ports
->port
, ports
->port2
);
2406 static char const *show_access_list_rule(int extended
, ip_filter_rulet
*rule
)
2408 static char buf
[256];
2411 p
+= sprintf(p
, " %s", rule
->action
== FILTER_ACTION_PERMIT
? "permit" : "deny");
2414 struct protoent
*proto
= getprotobynumber(rule
->proto
);
2415 p
+= sprintf(p
, " %s", proto
? proto
->p_name
: "ERR");
2418 p
+= show_ip_wild(p
, rule
->src_ip
, rule
->src_wild
);
2422 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2423 p
+= show_ports(p
, &rule
->src_ports
);
2425 p
+= show_ip_wild(p
, rule
->dst_ip
, rule
->dst_wild
);
2426 if (rule
->proto
== IPPROTO_TCP
|| rule
->proto
== IPPROTO_UDP
)
2427 p
+= show_ports(p
, &rule
->dst_ports
);
2429 if (rule
->proto
== IPPROTO_TCP
&& (rule
->tcp_sflags
|| rule
->tcp_cflags
))
2431 if (rule
->tcp_flag_op
== FILTER_FLAG_OP_ANY
&&
2432 rule
->tcp_sflags
== (TCP_FLAG_ACK
|TCP_FLAG_FIN
) &&
2433 rule
->tcp_cflags
== TCP_FLAG_SYN
)
2435 p
+= sprintf(p
, " established");
2439 p
+= sprintf(p
, " match-%s", rule
->tcp_flag_op
== FILTER_FLAG_OP_ALL
? "all" : "any");
2440 if (rule
->tcp_sflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " +fin");
2441 if (rule
->tcp_cflags
& TCP_FLAG_FIN
) p
+= sprintf(p
, " -fin");
2442 if (rule
->tcp_sflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " +syn");
2443 if (rule
->tcp_cflags
& TCP_FLAG_SYN
) p
+= sprintf(p
, " -syn");
2444 if (rule
->tcp_sflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " +rst");
2445 if (rule
->tcp_cflags
& TCP_FLAG_RST
) p
+= sprintf(p
, " -rst");
2446 if (rule
->tcp_sflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " +psh");
2447 if (rule
->tcp_cflags
& TCP_FLAG_PSH
) p
+= sprintf(p
, " -psh");
2448 if (rule
->tcp_sflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " +ack");
2449 if (rule
->tcp_cflags
& TCP_FLAG_ACK
) p
+= sprintf(p
, " -ack");
2450 if (rule
->tcp_sflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " +urg");
2451 if (rule
->tcp_cflags
& TCP_FLAG_URG
) p
+= sprintf(p
, " -urg");
2458 ip_filter_rulet
*access_list_rule_ext(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2460 static ip_filter_rulet rule
;
2461 struct in_addr addr
;
2465 if (CLI_HELP_REQUESTED
)
2469 cli_arg_help(cli
, 0,
2470 "ip", "Match IP packets",
2471 "tcp", "Match TCP packets",
2472 "udp", "Match UDP packets",
2478 // *sigh*, too darned complex
2479 cli_arg_help(cli
, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL
);
2485 cli_print(cli
, "Specify rule details");
2489 memset(&rule
, 0, sizeof(rule
));
2490 rule
.action
= (command
[0] == 'p')
2491 ? FILTER_ACTION_PERMIT
2492 : FILTER_ACTION_DENY
;
2494 if (MATCH("ip", argv
[0]))
2495 rule
.proto
= IPPROTO_IP
;
2496 else if (MATCH("udp", argv
[0]))
2497 rule
.proto
= IPPROTO_UDP
;
2498 else if (MATCH("tcp", argv
[0]))
2499 rule
.proto
= IPPROTO_TCP
;
2502 cli_print(cli
, "Invalid protocol \"%s\"", argv
[0]);
2506 for (a
= 1, i
= 0; i
< 2; i
++)
2510 ip_filter_portt
*port
;
2515 wild
= &rule
.src_wild
;
2516 port
= &rule
.src_ports
;
2521 wild
= &rule
.dst_wild
;
2522 port
= &rule
.dst_ports
;
2525 cli_print(cli
, "Specify destination");
2530 if (MATCH("any", argv
[a
]))
2533 *wild
= INADDR_BROADCAST
;
2536 else if (MATCH("host", argv
[a
]))
2540 cli_print(cli
, "Specify host ip address");
2544 if (!inet_aton(argv
[a
], &addr
))
2546 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2558 cli_print(cli
, "Specify %s ip address and wildcard", i
? "destination" : "source");
2562 if (!inet_aton(argv
[a
], &addr
))
2564 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2570 if (!inet_aton(argv
[++a
], &addr
))
2572 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[a
]);
2576 *wild
= addr
.s_addr
;
2579 if (rule
.proto
== IPPROTO_IP
|| a
>= argc
)
2583 if (MATCH("eq", argv
[a
]))
2584 port
->op
= FILTER_PORT_OP_EQ
;
2585 else if (MATCH("neq", argv
[a
]))
2586 port
->op
= FILTER_PORT_OP_NEQ
;
2587 else if (MATCH("gt", argv
[a
]))
2588 port
->op
= FILTER_PORT_OP_GT
;
2589 else if (MATCH("lt", argv
[a
]))
2590 port
->op
= FILTER_PORT_OP_LT
;
2591 else if (MATCH("range", argv
[a
]))
2592 port
->op
= FILTER_PORT_OP_RANGE
;
2599 cli_print(cli
, "Specify port");
2603 if (!(port
->port
= atoi(argv
[a
])))
2605 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2610 if (port
->op
!= FILTER_PORT_OP_RANGE
)
2615 cli_print(cli
, "Specify port");
2619 if (!(port
->port2
= atoi(argv
[a
])) || port
->port2
< port
->port
)
2621 cli_print(cli
, "Invalid port \"%s\"", argv
[a
]);
2628 if (rule
.proto
== IPPROTO_TCP
&& a
< argc
)
2630 if (MATCH("established", argv
[a
]))
2632 rule
.tcp_flag_op
= FILTER_FLAG_OP_ANY
;
2633 rule
.tcp_sflags
= (TCP_FLAG_ACK
|TCP_FLAG_FIN
);
2634 rule
.tcp_cflags
= TCP_FLAG_SYN
;
2637 else if (!strcmp(argv
[a
], "match-any") || !strcmp(argv
[a
], "match-an") ||
2638 !strcmp(argv
[a
], "match-all") || !strcmp(argv
[a
], "match-al"))
2640 rule
.tcp_flag_op
= argv
[a
][7] == 'n'
2641 ? FILTER_FLAG_OP_ANY
2642 : FILTER_FLAG_OP_ALL
;
2646 cli_print(cli
, "Specify tcp flags");
2650 while (a
< argc
&& (argv
[a
][0] == '+' || argv
[a
][0] == '-'))
2654 f
= (argv
[a
][0] == '+') ? &rule
.tcp_sflags
: &rule
.tcp_cflags
;
2656 if (MATCH("fin", &argv
[a
][1])) *f
|= TCP_FLAG_FIN
;
2657 else if (MATCH("syn", &argv
[a
][1])) *f
|= TCP_FLAG_SYN
;
2658 else if (MATCH("rst", &argv
[a
][1])) *f
|= TCP_FLAG_RST
;
2659 else if (MATCH("psh", &argv
[a
][1])) *f
|= TCP_FLAG_PSH
;
2660 else if (MATCH("ack", &argv
[a
][1])) *f
|= TCP_FLAG_ACK
;
2661 else if (MATCH("urg", &argv
[a
][1])) *f
|= TCP_FLAG_URG
;
2664 cli_print(cli
, "Invalid tcp flag \"%s\"", argv
[a
]);
2675 cli_print(cli
, "Invalid flag \"%s\"", argv
[a
]);
2682 ip_filter_rulet
*access_list_rule_std(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2684 static ip_filter_rulet rule
;
2685 struct in_addr addr
;
2687 if (CLI_HELP_REQUESTED
)
2691 cli_arg_help(cli
, argv
[0][1],
2692 "A.B.C.D", "Source address",
2693 "any", "Any source address",
2694 "host", "Source host",
2700 if (MATCH("any", argv
[0]))
2702 if (argc
== 2 && !argv
[1][1])
2703 cli_arg_help(cli
, 1, NULL
);
2705 else if (MATCH("host", argv
[0]))
2709 cli_arg_help(cli
, argv
[1][1],
2710 "A.B.C.D", "Host address",
2713 else if (argc
== 3 && !argv
[2][1])
2714 cli_arg_help(cli
, 1, NULL
);
2720 cli_arg_help(cli
, 1,
2721 "A.B.C.D", "Wildcard bits",
2724 else if (argc
== 3 && !argv
[2][1])
2725 cli_arg_help(cli
, 1, NULL
);
2733 cli_print(cli
, "Specify rule details");
2737 memset(&rule
, 0, sizeof(rule
));
2738 rule
.action
= (command
[0] == 'p')
2739 ? FILTER_ACTION_PERMIT
2740 : FILTER_ACTION_DENY
;
2742 rule
.proto
= IPPROTO_IP
;
2743 if (MATCH("any", argv
[0]))
2745 rule
.src_ip
= INADDR_ANY
;
2746 rule
.src_wild
= INADDR_BROADCAST
;
2748 else if (MATCH("host", argv
[0]))
2752 cli_print(cli
, "Specify host ip address");
2756 if (!inet_aton(argv
[1], &addr
))
2758 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2762 rule
.src_ip
= addr
.s_addr
;
2763 rule
.src_wild
= INADDR_ANY
;
2769 cli_print(cli
, "Specify source ip address and wildcard");
2773 if (!inet_aton(argv
[0], &addr
))
2775 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[0]);
2779 rule
.src_ip
= addr
.s_addr
;
2783 if (!inet_aton(argv
[1], &addr
))
2785 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
2789 rule
.src_wild
= addr
.s_addr
;
2792 rule
.src_wild
= INADDR_ANY
;
2798 static int cmd_ip_access_list_rule(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
2801 ip_filter_rulet
*rule
= ip_filters
[filt
].extended
2802 ? access_list_rule_ext(cli
, command
, argv
, argc
)
2803 : access_list_rule_std(cli
, command
, argv
, argc
);
2808 for (i
= 0; i
< MAXFILTER_RULES
- 1; i
++) // -1: list always terminated by empty rule
2810 if (!ip_filters
[filt
].rules
[i
].action
)
2812 memcpy(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
));
2816 if (!memcmp(&ip_filters
[filt
].rules
[i
], rule
, sizeof(*rule
)))
2820 cli_print(cli
, "Too many rules");
2824 // Convert a string in the form of abcd.ef12.3456 into char[6]
2825 void parsemac(char *string
, char mac
[6])
2827 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)
2829 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)