1 // L2TPNS Command Line Interface
2 // $Id: cli.c,v 1.5 2004-06-23 03:52:24 fred_nerk Exp $
14 #include <arpa/inet.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
29 extern tunnelt
*tunnel
;
30 extern sessiont
*session
;
31 extern radiust
*radius
;
32 extern ippoolt
*ip_address_pool
;
33 extern struct Tstats
*_statistics
;
34 struct cli_def
*cli
= NULL
;
36 extern int clifd
, udpfd
, tapfd
, snoopfd
, ifrfd
, cluster_sockfd
;
38 extern sessionidt
*cli_session_kill
;
39 extern tunnelidt
*cli_tunnel_kill
;
40 extern struct configt
*config
;
41 extern struct config_descriptt config_values
[];
42 extern char hostname
[];
44 extern struct Tringbuffer
*ringbuffer
;
47 char *rcs_id
= "$Id: cli.c,v 1.5 2004-06-23 03:52:24 fred_nerk Exp $";
49 char *debug_levels
[] = {
73 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
74 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
75 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
76 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 int cmd_show_cluster(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 int regular_stuff(struct cli_def
*cli
);
106 struct cli_command
*c
;
107 struct cli_command
*c2
;
109 struct sockaddr_in addr
;
113 c
= cli_register_command(cli
, NULL
, "show", NULL
, NULL
);
114 cli_register_command(cli
, c
, "banana", cmd_show_banana
, "Show a banana");
116 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, "Show BGP status");
118 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, "Show cluster information");
119 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, "Show contents of the IP cache");
120 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, "List all installed plugins");
121 cli_register_command(cli
, c
, "pool", cmd_show_pool
, "Show the IP address allocation pool");
122 cli_register_command(cli
, c
, "radius", cmd_show_radius
, "Show active radius queries");
123 cli_register_command(cli
, c
, "running-config", cmd_show_run
, "Show the currently running configuration");
124 cli_register_command(cli
, c
, "session", cmd_show_session
, "Show a list of sessions or details for a single session");
125 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, "List all token bucket filters in use");
126 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, "List all throttled sessions and associated TBFs");
127 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, "Show a list of tunnels or details for a single tunnel");
128 cli_register_command(cli
, c
, "users", cmd_show_users
, "Show a list of all connected users or details of selected user");
129 cli_register_command(cli
, c
, "version", cmd_show_version
, "Show currently running software version");
131 c2
= cli_register_command(cli
, c
, "histogram", NULL
, NULL
);
132 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, "Show histogram of session idle times");
133 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, "Show histogram of session durations");
136 cli_register_command(cli
, c
, "counters", cmd_show_counters
, "Display all the internal counters and running totals");
138 c
= cli_register_command(cli
, NULL
, "clear", NULL
, NULL
);
139 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, "Clear internal counters");
142 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, "Show uptime and bandwidth utilisation");
144 c
= cli_register_command(cli
, NULL
, "write", NULL
, NULL
);
145 cli_register_command(cli
, c
, "memory", cmd_write_memory
, "Save the running config to flash");
146 cli_register_command(cli
, c
, "terminal", cmd_show_run
, "Show the running config");
148 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, "Temporarily enable interception for a user");
149 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, "Temporarily enable throttling for a user");
150 cli_register_command(cli
, NULL
, "debug", cmd_debug
, "Set the level of logging that is shown on the console");
152 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, NULL
);
153 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, "Withdraw routes from BGP peer");
155 c
= cli_register_command(cli
, NULL
, "no", NULL
, NULL
);
156 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, "Temporarily disable interception for a user");
157 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, "Temporarily disable throttling for a user");
158 cli_register_command(cli
, c
, "debug", cmd_no_debug
, "Turn off logging of a certain level of debugging");
159 c2
= cli_register_command(cli
, c
, "suspend", NULL
, NULL
);
160 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, "Advertise routes to BGP peer");
162 c
= cli_register_command(cli
, NULL
, "drop", NULL
, NULL
);
163 cli_register_command(cli
, c
, "user", cmd_drop_user
, "Disconnect a user");
164 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, "Disconnect a tunnel and all sessions on that tunnel");
165 cli_register_command(cli
, c
, "session", cmd_drop_session
, "Disconnect a session");
167 c
= cli_register_command(cli
, NULL
, "restart", NULL
, NULL
);
168 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, "Restart BGP");
170 c
= cli_register_command(cli
, NULL
, "load", NULL
, NULL
);
171 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, "Load a plugin");
173 c
= cli_register_command(cli
, NULL
, "remove", NULL
, NULL
);
174 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, "Remove a plugin");
176 cli_register_command(cli
, NULL
, "set", cmd_set
, "Set a configuration variable");
178 // Enable regular processing
179 cli_regular(cli
, regular_stuff
);
181 if (!(f
= fopen(CLIUSERS
, "r")))
183 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
187 while (fgets(buf
, 4096, f
))
190 if (*buf
== '#') continue;
191 if ((p
= strchr(buf
, '\r'))) *p
= 0;
192 if ((p
= strchr(buf
, '\n'))) *p
= 0;
194 if (!(p
= strchr((char *)buf
, ':'))) continue;
196 cli_allow_user(cli
, buf
, p
);
197 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
202 memset(&addr
, 0, sizeof(addr
));
203 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
204 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
207 // Set cli fd as non-blocking
208 flags
= fcntl(clifd
, F_GETFL
, 0);
209 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
211 addr
.sin_family
= AF_INET
;
212 addr
.sin_port
= htons(23);
213 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
215 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
221 void cli_do(int sockfd
)
226 if (config
->scheduler_fifo
)
229 struct sched_param params
= {0};
230 params
.sched_priority
= 0;
231 if ((ret
= sched_setscheduler(0, SCHED_OTHER
, ¶ms
)) == 0)
233 log(3, 0, 0, 0, "Dropped FIFO scheduler\n");
237 log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno
));
238 log(0, 0, 0, 0, "This is probably really really bad.\n");
242 signal(SIGPIPE
, SIG_DFL
);
243 signal(SIGCHLD
, SIG_DFL
);
244 signal(SIGHUP
, SIG_DFL
);
245 signal(SIGUSR1
, SIG_DFL
);
246 signal(SIGQUIT
, SIG_DFL
);
247 signal(SIGKILL
, SIG_DFL
);
248 signal(SIGALRM
, SIG_DFL
);
249 signal(SIGTERM
, SIG_DFL
);
252 if (udpfd
) close(udpfd
); udpfd
= 0;
253 if (tapfd
) close(tapfd
); tapfd
= 0;
254 if (snoopfd
) close(snoopfd
); snoopfd
= 0;
255 for (i
= 0; i
< config
->num_radfds
; i
++)
256 if (radfds
[i
]) close(radfds
[i
]);
257 if (ifrfd
) close(ifrfd
); ifrfd
= 0;
258 if (cluster_sockfd
) close(cluster_sockfd
); cluster_sockfd
= 0;
259 if (clifd
) close(clifd
); clifd
= 0;
261 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
262 if (bgp_peers
[i
].sock
!= -1)
263 close(bgp_peers
[i
].sock
);
266 log(3, 0, 0, 0, "Accepted connection to CLI\n");
271 debug_rb_tail
= ringbuffer
->tail
;
273 memset(&debug_flags
, 0, sizeof(debug_flags
));
274 debug_flags
.critical
= 1;
278 snprintf(prompt
, 1005, "l2tpns> ");
279 cli_loop(cli
, sockfd
, prompt
);
283 log(3, 0, 0, 0, "Closed CLI connection\n");
287 void cli_print_log(struct cli_def
*cli
, char *string
)
289 log(3, 0, 0, 0, "%s\n", string
);
292 void cli_do_file(FILE *fh
)
294 log(3, 0, 0, 0, "Reading configuration file\n");
295 cli_print_callback(cli
, cli_print_log
);
297 cli_print_callback(cli
, NULL
);
300 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
308 // Show individual session
309 for (i
= 0; i
< argc
; i
++)
313 if (!s
|| s
> MAXSESSION
)
315 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
318 cli_print(cli
, "\r\nSession %d:", s
);
319 cli_print(cli
, " User: %s", session
[s
].user
[0] ? session
[s
].user
: "none");
320 cli_print(cli
, " Calling Num: %s", session
[s
].calling
);
321 cli_print(cli
, " Called Num: %s", session
[s
].called
);
322 cli_print(cli
, " Tunnel ID: %d", session
[s
].tunnel
);
323 cli_print(cli
, " IP address: %s", inet_toa(htonl(session
[s
].ip
)));
324 cli_print(cli
, " HSD sid: %lu", session
[s
].sid
);
325 cli_print(cli
, " Idle time: %u seconds", abs(time_now
- session
[s
].last_packet
));
326 cli_print(cli
, " Next Recv: %u", session
[s
].nr
);
327 cli_print(cli
, " Next Send: %u", session
[s
].ns
);
328 cli_print(cli
, " Bytes In/Out: %lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
329 cli_print(cli
, " Pkts In/Out: %lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
330 cli_print(cli
, " MRU: %d", session
[s
].mru
);
331 cli_print(cli
, " Radius Session: %u", session
[s
].radius
);
332 cli_print(cli
, " Rx Speed: %lu", session
[s
].rx_connect_speed
);
333 cli_print(cli
, " Tx Speed: %lu", session
[s
].tx_connect_speed
);
334 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
335 cli_print(cli
, " Intercepted: %s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
337 cli_print(cli
, " Intercepted: no");
338 cli_print(cli
, " Throttled: %s", session
[s
].throttle
? "YES" : "no");
339 cli_print(cli
, " Walled Garden: %s", session
[s
].walled_garden
? "YES" : "no");
340 cli_print(cli
, " Filter BucketI: %d", session
[s
].tbf_in
);
341 cli_print(cli
, " Filter BucketO: %d", session
[s
].tbf_out
);
347 cli_print(cli
, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
361 for (i
= 1; i
< MAXSESSION
; i
++)
363 char *userip
, *tunnelip
;
364 if (!session
[i
].opened
) continue;
365 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
366 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
367 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
370 session
[i
].user
[0] ? session
[i
].user
: "*",
372 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
373 (session
[i
].throttle
) ? "Y" : "N",
374 (session
[i
].walled_garden
) ? "Y" : "N",
375 abs(time_now
- (unsigned long)session
[i
].opened
),
376 (unsigned long)session
[i
].total_cout
,
377 (unsigned long)session
[i
].total_cin
,
378 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
380 session
[i
].calling
[0] ? session
[i
].calling
: "*");
381 if (userip
) free(userip
);
382 if (tunnelip
) free(tunnelip
);
387 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
389 int i
, x
, show_all
= 0;
401 if (strcmp(argv
[0], "all") == 0)
407 // Show individual tunnel
408 for (i
= 0; i
< argc
; i
++)
413 if (!t
|| t
> MAXTUNNEL
)
415 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
418 cli_print(cli
, "\r\nTunnel %d:", t
);
419 cli_print(cli
, " State: %s", states
[tunnel
[t
].state
]);
420 cli_print(cli
, " Hostname: %s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
421 cli_print(cli
, " Remote IP: %s", inet_toa(htonl(tunnel
[t
].ip
)));
422 cli_print(cli
, " Remote Port: %d", tunnel
[t
].port
);
423 cli_print(cli
, " Rx Window: %u", tunnel
[t
].window
);
424 cli_print(cli
, " Next Recv: %u", tunnel
[t
].nr
);
425 cli_print(cli
, " Next Send: %u", tunnel
[t
].ns
);
426 cli_print(cli
, " Queue Len: %u", tunnel
[t
].controlc
);
427 cli_print(cli
, " Last Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
429 for (x
= 0; x
< MAXSESSION
; x
++)
430 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
431 sprintf(s
, "%s%u ", s
, x
);
432 cli_print(cli
, " Sessions: %s", s
);
438 // Show tunnel summary
439 cli_print(cli
, "%s %s %s %s %s",
445 for (i
= 1; i
< MAXTUNNEL
; i
++)
448 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
|| !tunnel
[i
].hostname
[0])) continue;
450 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
451 cli_print(cli
, "%d %s %s %s %d",
453 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
454 inet_toa(htonl(tunnel
[i
].ip
)),
455 states
[tunnel
[i
].state
],
461 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
467 for (i
= 0; i
< MAXSESSION
; i
++)
469 if (!session
[i
].opened
) continue;
470 if (!session
[i
].user
[0]) continue;
474 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
476 if (strcmp(argv
[j
], session
[i
].user
) == 0)
478 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
479 sargv
[sargc
] = sid
[sargc
];
487 cli_print(cli
, "%s", session
[i
].user
);
491 return cmd_show_session(cli
, "users", sargv
, sargc
);
496 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
498 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
499 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
500 GET_STAT(tap_rx_bytes
),
501 GET_STAT(tap_rx_packets
),
502 GET_STAT(tap_rx_errors
));
503 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
504 GET_STAT(tap_tx_bytes
),
505 GET_STAT(tap_tx_packets
),
506 GET_STAT(tap_tx_errors
));
509 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
510 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
511 GET_STAT(tunnel_rx_bytes
),
512 GET_STAT(tunnel_rx_packets
),
513 GET_STAT(tunnel_rx_errors
),
515 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
516 GET_STAT(tunnel_tx_bytes
),
517 GET_STAT(tunnel_tx_packets
),
518 GET_STAT(tunnel_tx_errors
),
519 GET_STAT(tunnel_retries
));
522 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
523 cli_print(cli
, "-----------------------------------------");
524 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
525 cli_print(cli
, "%-30s%lu", "arp_errors", GET_STAT(arp_errors
));
526 cli_print(cli
, "%-30s%lu", "arp_replies", GET_STAT(arp_replies
));
527 cli_print(cli
, "%-30s%lu", "arp_discarded", GET_STAT(arp_discarded
));
528 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
529 cli_print(cli
, "%-30s%lu", "arp_recv", GET_STAT(arp_recv
));
530 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
531 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
532 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
533 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
534 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
535 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
536 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
537 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
538 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
539 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
540 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
541 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
542 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
546 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
547 cli_print(cli
, "-----------------------------------------");
548 cli_print(cli
, "%-30s%lu", "call_processtap", GET_STAT(call_processtap
));
549 cli_print(cli
, "%-30s%lu", "call_processarp", GET_STAT(call_processarp
));
550 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
551 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
552 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
553 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
554 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
555 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
556 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
557 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
558 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
559 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
560 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
561 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
562 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
563 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
564 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
565 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
566 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
567 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
568 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
569 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
570 cli_print(cli
, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address
));
571 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
572 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
573 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
574 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
579 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
581 cli_print(cli
, "L2TPNS %s", VERSION
);
582 cli_print(cli
, "ID: %s", rcs_id
);
586 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
589 int used
= 0, free
= 0, show_all
= 0;
592 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
596 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
597 for (i
= 0; i
< MAXIPPOOL
; i
++)
599 if (!ip_address_pool
[i
].address
) continue;
600 if (ip_address_pool
[i
].assigned
)
602 cli_print(cli
, "%-15s Y %8d %s",
603 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
609 if (ip_address_pool
[i
].last
)
610 cli_print(cli
, "%-15s N %8s [%s] %ds",
611 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
612 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
614 cli_print(cli
, "%-15s N", inet_toa(htonl(ip_address_pool
[i
].address
)));
621 cli_print(cli
, "(Not displaying unused addresses)");
623 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
627 void print_save_config(struct cli_def
*cli
, char *string
)
630 fprintf(save_config_fh
, "%s\n", string
);
633 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
635 if ((save_config_fh
= fopen(config
->config_file
, "w")))
637 cli_print(cli
, "Writing configuration");
638 cli_print_callback(cli
, print_save_config
);
639 cmd_show_run(cli
, command
, argv
, argc
);
640 cli_print_callback(cli
, NULL
);
641 fclose(save_config_fh
);
645 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
650 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
654 cli_print(cli
, "# Current configuration:");
656 for (i
= 0; config_values
[i
].key
; i
++)
658 void *value
= ((void *)config
) + config_values
[i
].offset
;
659 if (config_values
[i
].type
== STRING
)
660 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
661 else if (config_values
[i
].type
== IP
)
662 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
663 else if (config_values
[i
].type
== SHORT
)
664 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
665 else if (config_values
[i
].type
== BOOL
)
666 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
667 else if (config_values
[i
].type
== INT
)
668 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
669 else if (config_values
[i
].type
== UNSIGNED_LONG
)
670 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
673 cli_print(cli
, "# Plugins");
674 for (i
= 0; i
< MAXPLUGINS
; i
++)
676 if (*config
->plugins
[i
])
678 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
682 cli_print(cli
, "# end");
686 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
697 int i
, free
= 0, used
= 0, show_all
= 0;
700 cli_print(cli
, "%6s%5s%6s%9s%9s%4s", "Radius", "Sock", "State", "Session", "Retry", "Try");
704 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
707 for (i
= 1; i
< MAXRADIUS
; i
++)
709 if (radius
[i
].state
== RADIUSNULL
)
714 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
716 cli_print(cli
, "%6d%5d%6s%9d%9u%4d",
719 states
[radius
[i
].state
],
725 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
730 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
733 cli_print(cli
, "Plugins currently loaded:");
734 for (i
= 0; i
< MAXPLUGINS
; i
++)
736 if (*config
->plugins
[i
])
738 cli_print(cli
, " %s", config
->plugins
[i
]);
744 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
747 cli_print(cli
, "Token bucket filters:");
748 cli_print(cli
, "%-6s %8s %-4s", "ID", "Handle", "Used");
749 for (i
= 0; i
< MAXSESSION
; i
++)
751 if (!session
[i
].throttle
)
754 cli_print(cli
, "%-6d %8d %8d",
762 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
764 cli_print(cli
, " _\n"
770 " ( \\ `. `-. _,.-:\\\n"
771 " \\ \\ `. `-._ __..--' ,-';/\n"
772 " \\ `. `-. `-..___..---' _.--' ,'/\n"
773 " `. `. `-._ __..--' ,' /\n"
774 " `. `-_ ``--..'' _.-' ,'\n"
775 " `-_ `-.___ __,--' ,'\n"
776 " `-.__ `----\"\"\" __.-'\n"
777 "hh `--..____..--'");
782 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
784 cli_print(cli
, "Counters cleared");
785 SET_STAT(last_reset
, time(NULL
));
789 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
794 if (!config
->cluster_iam_master
)
796 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
801 cli_print(cli
, "Specify a user to drop");
804 for (i
= 0; i
< argc
; i
++)
806 if (strchr(argv
[i
], '?'))
808 cli_print(cli
, "username ...");
813 for (i
= 0; i
< argc
; i
++)
815 if (!(s
= sessionbyuser(argv
[i
])))
817 cli_print(cli
, "User %s is not connected", argv
[i
]);
821 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
825 cli_print(cli
, "Dropping user %s", session
[s
].user
);
826 for (x
= 0; x
< MAXSESSION
; x
++)
828 if (!cli_session_kill
[x
])
830 cli_session_kill
[x
] = s
;
840 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
845 if (!config
->cluster_iam_master
)
847 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
852 cli_print(cli
, "Specify a tunnel to drop");
855 for (i
= 0; i
< argc
; i
++)
857 if (strchr(argv
[i
], '?'))
859 cli_print(cli
, "tunnel_id ...");
864 for (i
= 0; i
< argc
; i
++)
868 if ((tid
= atol(argv
[i
])) <= 0 || (tid
> MAXTUNNEL
))
870 cli_print(cli
, "Invalid tunnel ID (%d - %d)", 0, MAXTUNNEL
);
876 cli_print(cli
, "Tunnel %d is not connected", tid
);
882 cli_print(cli
, "Tunnel %d is already being shut down", tid
);
886 for (x
= 0; x
< MAXTUNNEL
; x
++)
888 if (!cli_tunnel_kill
[x
])
890 cli_tunnel_kill
[x
] = tid
;
891 cli_print(cli
, "Tunnel %d shut down (%s)", tid
, tunnel
[tid
].hostname
);
900 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
905 if (!config
->cluster_iam_master
)
907 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
912 cli_print(cli
, "Specify a session id to drop");
915 for (i
= 0; i
< argc
; i
++)
917 if (strchr(argv
[i
], '?'))
919 cli_print(cli
, "session_id ...");
924 for (i
= 0; i
< argc
; i
++)
926 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
928 cli_print(cli
, "Invalid session ID (%d - %d)", 0, MAXSESSION
);
932 if (session
[s
].opened
&& !session
[s
].die
)
935 for (x
= 0; x
< MAXSESSION
; x
++)
937 if (!cli_session_kill
[x
])
939 cli_session_kill
[x
] = s
;
943 cli_print(cli
, "Dropping session %d", s
);
947 cli_print(cli
, "Session %d is not active.", s
);
954 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
961 if (!config
->cluster_iam_master
)
963 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
969 cli_print(cli
, "Specify username ip port");
973 for (i
= 0; i
< argc
; i
++)
975 if (strchr(argv
[i
], '?'))
977 cli_print(cli
, "username ip port");
983 if (!(s
= sessionbyuser(argv
[0])))
985 cli_print(cli
, "User %s is not connected", argv
[0]);
989 ip
= inet_addr(argv
[1]);
990 if (!ip
|| ip
== INADDR_NONE
)
992 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
996 port
= atoi(argv
[2]);
999 cli_print(cli
, "Invalid port %s", argv
[2]);
1003 session
[s
].snoop_ip
= ip
;
1004 session
[s
].snoop_port
= port
;
1006 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(session
[s
].snoop_ip
), session
[s
].snoop_port
);
1010 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1015 if (!config
->cluster_iam_master
)
1017 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1023 cli_print(cli
, "Specify a user");
1026 for (i
= 0; i
< argc
; i
++)
1028 if (strchr(argv
[i
], '?'))
1030 cli_print(cli
, "username ...");
1035 for (i
= 0; i
< argc
; i
++)
1037 if (!(s
= sessionbyuser(argv
[i
])))
1039 cli_print(cli
, "User %s is not connected", argv
[i
]);
1042 session
[s
].snoop_ip
= 0;
1043 session
[s
].snoop_port
= 0;
1045 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1050 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1055 if (!config
->cluster_iam_master
)
1057 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1062 cli_print(cli
, "Specify a user");
1065 for (i
= 0; i
< argc
; i
++)
1067 if (strchr(argv
[i
], '?'))
1069 cli_print(cli
, "username ...");
1074 for (i
= 0; i
< argc
; i
++)
1076 if (!(s
= sessionbyuser(argv
[i
])))
1078 cli_print(cli
, "User %s is not connected", argv
[i
]);
1081 if (!throttle_session(s
, config
->rl_rate
))
1082 cli_print(cli
, "Error throttling %s", argv
[i
]);
1084 cli_print(cli
, "Throttling user %s", argv
[i
]);
1089 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1094 if (!config
->cluster_iam_master
)
1096 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1101 cli_print(cli
, "Specify a user");
1104 for (i
= 0; i
< argc
; i
++)
1106 if (strchr(argv
[i
], '?'))
1108 cli_print(cli
, "username ...");
1113 for (i
= 0; i
< argc
; i
++)
1115 if (!(s
= sessionbyuser(argv
[i
])))
1117 cli_print(cli
, "User %s is not connected", argv
[i
]);
1120 throttle_session(s
, 0);
1122 cli_print(cli
, "unthrottling user %s", argv
[i
]);
1127 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1133 cli_print(cli
, "Currently debugging: ");
1134 if (debug_flags
.critical
) cli_print(cli
, "critical ");
1135 if (debug_flags
.error
) cli_print(cli
, "error ");
1136 if (debug_flags
.warning
) cli_print(cli
, "warning ");
1137 if (debug_flags
.info
) cli_print(cli
, "info ");
1138 if (debug_flags
.calls
) cli_print(cli
, "calls ");
1139 if (debug_flags
.data
) cli_print(cli
, "data ");
1144 for (i
= 0; i
< argc
; i
++)
1146 if (*argv
[i
] == '?')
1148 cli_print(cli
, "Possible debugging states are:");
1149 cli_print(cli
, " critical");
1150 cli_print(cli
, " error");
1151 cli_print(cli
, " warning");
1152 cli_print(cli
, " info");
1153 cli_print(cli
, " calls");
1154 cli_print(cli
, " data");
1159 for (i
= 0; i
< argc
; i
++)
1161 if (strcasecmp(argv
[i
], "critical") == 0) debug_flags
.critical
= 1;
1162 if (strcasecmp(argv
[i
], "error") == 0) debug_flags
.error
= 1;
1163 if (strcasecmp(argv
[i
], "warning") == 0) debug_flags
.warning
= 1;
1164 if (strcasecmp(argv
[i
], "info") == 0) debug_flags
.info
= 1;
1165 if (strcasecmp(argv
[i
], "calls") == 0) debug_flags
.calls
= 1;
1166 if (strcasecmp(argv
[i
], "data") == 0) debug_flags
.data
= 1;
1167 if (strcasecmp(argv
[i
], "all") == 0)
1169 memset(&debug_flags
, 1, sizeof(debug_flags
));
1170 debug_flags
.data
= 0;
1177 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1181 for (i
= 0; i
< argc
; i
++)
1183 if (strcasecmp(argv
[i
], "critical") == 0) debug_flags
.critical
= 0;
1184 if (strcasecmp(argv
[i
], "error") == 0) debug_flags
.error
= 0;
1185 if (strcasecmp(argv
[i
], "warning") == 0) debug_flags
.warning
= 0;
1186 if (strcasecmp(argv
[i
], "info") == 0) debug_flags
.info
= 0;
1187 if (strcasecmp(argv
[i
], "calls") == 0) debug_flags
.calls
= 0;
1188 if (strcasecmp(argv
[i
], "data") == 0) debug_flags
.data
= 0;
1189 if (strcasecmp(argv
[i
], "all") == 0) memset(&debug_flags
, 0, sizeof(debug_flags
));
1195 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1197 int i
, firstfree
= 0;
1200 cli_print(cli
, "Specify a plugin to load");
1204 for (i
= 0; i
< MAXPLUGINS
; i
++)
1206 if (!*config
->plugins
[i
] && !firstfree
)
1208 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1210 cli_print(cli
, "Plugin is already loaded");
1217 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1218 config
->reload_config
= 1;
1219 cli_print(cli
, "Loading plugin %s", argv
[0]);
1225 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1231 cli_print(cli
, "Specify a plugin to remove");
1235 for (i
= 0; i
< MAXPLUGINS
; i
++)
1237 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1239 config
->reload_config
= 1;
1240 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1245 cli_print(cli
, "Plugin is not loaded");
1249 char *duration(time_t seconds
)
1251 static char *buf
= NULL
;
1252 if (!buf
) buf
= calloc(64, 1);
1254 if (seconds
> 86400)
1255 sprintf(buf
, "%d days", (int)(seconds
/ 86400.0));
1256 else if (seconds
> 60)
1257 sprintf(buf
, "%02d:%02lu", (int)(seconds
/ 3600.0), seconds
% 60);
1259 sprintf(buf
, "%lu sec", seconds
);
1263 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1266 char buf
[100], *p
= buf
, *loads
[3];
1267 int i
, num_sessions
= 0;
1270 fh
= fopen("/proc/loadavg", "r");
1271 fgets(buf
, 100, fh
);
1274 for (i
= 0; i
< 3; i
++)
1275 loads
[i
] = strdup(strsep(&p
, " "));
1278 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1280 for (i
= 1; i
< MAXSESSION
; i
++)
1281 if (session
[i
].opened
) num_sessions
++;
1283 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1285 duration(abs(time_now
- config
->start_time
)),
1287 loads
[0], loads
[1], loads
[2]
1289 for (i
= 0; i
< 3; i
++)
1290 if (loads
[i
]) free(loads
[i
]);
1292 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1297 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1303 cli_print(cli
, "Usage: set <variable> <value>");
1307 for (i
= 0; config_values
[i
].key
; i
++)
1309 void *value
= ((void *)config
) + config_values
[i
].offset
;
1310 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1312 // Found a value to set
1313 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1314 switch (config_values
[i
].type
)
1317 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1320 *(int *)value
= atoi(argv
[1]);
1323 *(unsigned long *)value
= atol(argv
[1]);
1326 *(short *)value
= atoi(argv
[1]);
1329 *(unsigned *)value
= inet_addr(argv
[1]);
1332 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1338 cli_print(cli
, "Unknown variable type");
1341 config
->reload_config
= 1;
1346 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1350 int regular_stuff(struct cli_def
*cli
)
1352 int i
= debug_rb_tail
;
1356 while (i
!= ringbuffer
->tail
)
1358 int show_message
= 0;
1360 if (*ringbuffer
->buffer
[i
].message
)
1362 // Always show messages if we are doing general debug
1363 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1364 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1365 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1366 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1367 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1368 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1373 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1375 struct in_addr addr
;
1377 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1378 ipaddr
= inet_ntoa(addr
);
1380 cli_print(cli
, "\r%s-%s-%u-%u %s",
1381 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1383 ringbuffer
->buffer
[i
].tunnel
,
1384 ringbuffer
->buffer
[i
].session
,
1385 ringbuffer
->buffer
[i
].message
);
1390 if (++i
== ringbuffer
->tail
) break;
1391 if (i
== RINGBUFFER_SIZE
) i
= 0;
1394 debug_rb_tail
= ringbuffer
->tail
;