Display session's remainging time in CLI.
[l2tpns.git] / cli.c
1 // L2TPNS Command Line Interface
2 // vim: sw=8 ts=8
3
4 char const *cvs_name = "$Name: $";
5 char const *cvs_id_cli = "$Id: cli.c,v 1.76 2006/12/18 12:08:28 bodea Exp $";
6
7 #include <stdio.h>
8 #include <stddef.h>
9 #include <stdarg.h>
10 #include <unistd.h>
11 #include <sys/file.h>
12 #include <sys/stat.h>
13 #include <syslog.h>
14 #include <malloc.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <time.h>
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <dlfcn.h>
25 #include <netdb.h>
26 #include <libcli.h>
27
28 #include "l2tpns.h"
29 #include "constants.h"
30 #include "util.h"
31 #include "cluster.h"
32 #include "tbf.h"
33 #include "ll.h"
34 #ifdef BGP
35 #include "bgp.h"
36 #endif
37
38 extern tunnelt *tunnel;
39 extern bundlet *bundle;
40 extern sessiont *session;
41 extern radiust *radius;
42 extern ippoolt *ip_address_pool;
43 extern struct Tstats *_statistics;
44 static struct cli_def *cli = NULL;
45 extern configt *config;
46 extern config_descriptt config_values[];
47 #ifdef RINGBUFFER
48 extern struct Tringbuffer *ringbuffer;
49 #endif
50 extern struct cli_session_actions *cli_session_actions;
51 extern struct cli_tunnel_actions *cli_tunnel_actions;
52 extern tbft *filter_list;
53 extern ip_filtert *ip_filters;
54
55 struct
56 {
57 char critical;
58 char error;
59 char warning;
60 char info;
61 char calls;
62 char data;
63 } debug_flags;
64
65 #ifdef RINGBUFFER
66
67 static int debug_rb_tail;
68 static char *debug_levels[] = {
69 "CRIT",
70 "ERROR",
71 "WARN",
72 "INFO",
73 "CALL",
74 "DATA",
75 };
76
77 #endif
78
79 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
80 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
81 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
82 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
83 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
84 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
85 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
86 static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
87 static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
88 static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc);
89 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
90 static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
91 static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
92 static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
93 static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
94 static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
95 static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
96 static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
97 static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
98 static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
99 static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
100 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
101 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
102 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
103 static int cmd_shutdown(struct cli_def *cli, char *command, char **argv, int argc);
104 static int cmd_reload(struct cli_def *cli, char *command, char **argv, int argc);
105
106
107 static int regular_stuff(struct cli_def *cli);
108
109 #ifdef STATISTICS
110 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
111 static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
112 #endif /* STATISTICS */
113
114 #ifdef BGP
115 #define MODE_CONFIG_BGP 8
116 static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc);
117 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
118 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
119 static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc);
120 static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
121 static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
122 static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc);
123 #endif /* BGP */
124
125 #define MODE_CONFIG_NACL 9
126 static int cmd_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc);
127 static int cmd_no_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc);
128 static int cmd_ip_access_list_rule(struct cli_def *cli, char *command, char **argv, int argc);
129 static int cmd_filter(struct cli_def *cli, char *command, char **argv, int argc);
130 static int cmd_no_filter(struct cli_def *cli, char *command, char **argv, int argc);
131 static int cmd_show_access_list(struct cli_def *cli, char *command, char **argv, int argc);
132
133 /* match if b is a substr of a */
134 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
135
136 void init_cli(char *hostname)
137 {
138 FILE *f;
139 char buf[4096];
140 struct cli_command *c;
141 struct cli_command *c2;
142 int on = 1;
143 struct sockaddr_in addr;
144
145 cli = cli_init();
146 if (hostname && *hostname)
147 cli_set_hostname(cli, hostname);
148 else
149 cli_set_hostname(cli, "l2tpns");
150
151 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
152 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
153 #ifdef BGP
154 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
155 #endif /* BGP */
156 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
157 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
158 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
159 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
160 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
161 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Show the currently running configuration");
162 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
163 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
164 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
165 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
166 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");
167 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
168 cli_register_command(cli, c, "access-list", cmd_show_access_list, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show named access-list");
169
170 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
171 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
172 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
173
174 #ifdef STATISTICS
175 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
176
177 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
178 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
179 #endif
180
181 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
182 cli_register_command(cli, NULL, "shutdown", cmd_shutdown, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Shutdown l2tpns daemon and exit");
183 cli_register_command(cli, NULL, "reload", cmd_reload, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Reload configuration");
184
185 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
186 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
187 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
188
189 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable interception of a session");
190 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable throttling of a session");
191 cli_register_command(cli, NULL, "filter", cmd_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Add filtering to a session");
192 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
193
194 #ifdef BGP
195 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
196 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP neighbour");
197 #endif /* BGP */
198
199 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
200 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable interception of a session");
201 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable throttling of a session");
202 cli_register_command(cli, c, "filter", cmd_no_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Remove filtering from a session");
203 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
204
205 #ifdef BGP
206 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
207 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP neighbour");
208
209 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
210 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
211
212 c = cli_register_command(cli, NULL, "router", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
213 cli_register_command(cli, c, "bgp", cmd_router_bgp, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Configure BGP");
214
215 cli_register_command(cli, NULL, "neighbour", cmd_router_bgp_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Configure BGP neighbour");
216
217 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, NULL);
218 cli_register_command(cli, c, "neighbour", cmd_router_bgp_no_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Remove BGP neighbour");
219 #endif /* BGP */
220
221 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
222 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
223 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
224 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
225
226 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
227 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
228
229 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
230 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
231
232 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
233
234 c = cli_register_command(cli, NULL, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
235 cli_register_command(cli, c, "access-list", cmd_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Add named access-list");
236
237 cli_register_command(cli, NULL, "permit", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Permit rule");
238 cli_register_command(cli, NULL, "deny", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Deny rule");
239
240 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_CONFIG, NULL);
241 c2 = cli_register_command(cli, c, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
242 cli_register_command(cli, c2, "access-list", cmd_no_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove named access-list");
243
244 // Enable regular processing
245 cli_regular(cli, regular_stuff);
246
247 if (!(f = fopen(CLIUSERS, "r")))
248 {
249 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
250 }
251 else
252 {
253 while (fgets(buf, 4096, f))
254 {
255 char *p;
256 if (*buf == '#') continue;
257 if ((p = strchr(buf, '\r'))) *p = 0;
258 if ((p = strchr(buf, '\n'))) *p = 0;
259 if (!*buf) continue;
260 if (!(p = strchr((char *)buf, ':'))) continue;
261 *p++ = 0;
262 if (!strcmp(buf, "enable"))
263 {
264 cli_allow_enable(cli, p);
265 LOG(3, 0, 0, "Setting enable password\n");
266 }
267 else
268 {
269 cli_allow_user(cli, buf, p);
270 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
271 }
272 }
273 fclose(f);
274 }
275
276 memset(&addr, 0, sizeof(addr));
277 clifd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
278 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
279 {
280 int flags;
281 // Set cli fd as non-blocking
282 flags = fcntl(clifd, F_GETFL, 0);
283 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
284 }
285 addr.sin_family = AF_INET;
286 addr.sin_port = htons(23);
287 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
288 {
289 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
290 return;
291 }
292 listen(clifd, 10);
293 }
294
295 void cli_do(int sockfd)
296 {
297 int require_auth = 1;
298 struct sockaddr_in addr;
299 socklen_t l = sizeof(addr);
300
301 if (fork_and_close()) return;
302 if (getpeername(sockfd, (struct sockaddr *) &addr, &l) == 0)
303 {
304 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
305 LOG(require_auth ? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
306 fmtaddr(addr.sin_addr.s_addr, 0));
307 }
308 else
309 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
310
311 if (require_auth)
312 {
313 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
314 if (!cli->users) /* paranoia */
315 {
316 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
317 exit(0);
318 }
319 }
320 else
321 {
322 /* no username/pass required */
323 cli->users = 0;
324 }
325
326 #ifdef RINGBUFFER
327 debug_rb_tail = ringbuffer->tail;
328 #endif
329 memset(&debug_flags, 0, sizeof(debug_flags));
330 debug_flags.critical = 1;
331
332 cli_loop(cli, sockfd);
333
334 close(sockfd);
335 LOG(require_auth ? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
336 fmtaddr(addr.sin_addr.s_addr, 0));
337
338 exit(0);
339 }
340
341 static void cli_print_log(struct cli_def *cli, char *string)
342 {
343 LOG(3, 0, 0, "%s\n", string);
344 }
345
346 void cli_do_file(FILE *fh)
347 {
348 LOG(3, 0, 0, "Reading configuration file\n");
349 cli_print_callback(cli, cli_print_log);
350 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
351 cli_print_callback(cli, NULL);
352 }
353
354 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
355 {
356 va_list ap;
357 char *desc;
358 char buf[16];
359 char *p;
360
361 va_start(ap, entry);
362 while (entry)
363 {
364 /* allow one %d */
365 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
366 {
367 int v = va_arg(ap, int);
368 snprintf(buf, sizeof(buf), entry, v);
369 p = buf;
370 }
371 else
372 p = entry;
373
374 desc = va_arg(ap, char *);
375 if (desc && *desc)
376 cli_error(cli, " %-20s %s", p, desc);
377 else
378 cli_error(cli, " %s", p);
379
380 entry = desc ? va_arg(ap, char *) : 0;
381 }
382
383 va_end(ap);
384 if (cr_ok)
385 cli_error(cli, " <cr>");
386
387 return CLI_OK;
388 }
389
390 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
391 {
392 int i;
393
394 if (CLI_HELP_REQUESTED)
395 return cli_arg_help(cli, 1,
396 "<1-%d>", MAXSESSION-1, "Show specific session by id",
397 NULL);
398
399 time(&time_now);
400 if (argc > 0)
401 {
402 // Show individual session
403 for (i = 0; i < argc; i++)
404 {
405 unsigned int s, b_in, b_out;
406 s = atoi(argv[i]);
407 if (s <= 0 || s >= MAXSESSION)
408 {
409 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
410 continue;
411 }
412 cli_print(cli, "\r\nSession %d:", s);
413 cli_print(cli, "\tUser:\t\t%s", session[s].user[0] ? session[s].user : "none");
414 cli_print(cli, "\tCalling Num:\t%s", session[s].calling);
415 cli_print(cli, "\tCalled Num:\t%s", session[s].called);
416 cli_print(cli, "\tTunnel ID:\t%d", session[s].tunnel);
417 cli_print(cli, "\tPPP Phase:\t%s", ppp_phase(session[s].ppp.phase));
418 switch (session[s].ppp.phase)
419 {
420 case Establish:
421 cli_print(cli, "\t LCP state:\t%s", ppp_state(session[s].ppp.lcp));
422 break;
423
424 case Authenticate:
425 case Network:
426 cli_print(cli, "\t IPCP state:\t%s", ppp_state(session[s].ppp.ipcp));
427 cli_print(cli, "\t IPV6CP state:\t%s", ppp_state(session[s].ppp.ipv6cp));
428 cli_print(cli, "\t CCP state:\t%s", ppp_state(session[s].ppp.ccp));
429 }
430 cli_print(cli, "\tIP address:\t%s", fmtaddr(htonl(session[s].ip), 0));
431 cli_print(cli, "\tUnique SID:\t%u", session[s].unique_id);
432 cli_print(cli, "\tOpened:\t\t%u seconds", session[s].opened ? abs(time_now - session[s].opened) : 0);
433 cli_print(cli, "\tIdle time:\t%u seconds", session[s].last_packet ? abs(time_now - session[s].last_packet) : 0);
434 if (session[s].session_timeout)
435 {
436 clockt opened = session[s].opened;
437 if (session[s].bundle && bundle[session[s].bundle].num_of_links > 1)
438 opened = bundle[session[s].bundle].online_time;
439
440 cli_print(cli, "\tSess Timeout:\t%u seconds", session[s].session_timeout - (opened ? abs(time_now - opened) : 0));
441 }
442
443 if (session[s].idle_timeout)
444 cli_print(cli, "\tIdle Timeout:\t%u seconds", session[s].idle_timeout - (session[s].last_data ? abs(time_now - session[s].last_data) : 0));
445
446 if (session[s].timeout)
447 {
448 cli_print(cli, "\tRemaining time:\t%u",
449 (session[s].bundle && bundle[session[s].bundle].num_of_links > 1)
450 ? (unsigned) (session[s].timeout - bundle[session[s].bundle].online_time)
451 : (unsigned) (session[s].timeout - (time_now - session[s].opened)));
452 }
453
454 cli_print(cli, "\tBytes In/Out:\t%u/%u", session[s].cout, session[s].cin);
455 cli_print(cli, "\tPkts In/Out:\t%u/%u", session[s].pout, session[s].pin);
456 cli_print(cli, "\tMRU:\t\t%d", session[s].mru);
457 cli_print(cli, "\tRx Speed:\t%u", session[s].rx_connect_speed);
458 cli_print(cli, "\tTx Speed:\t%u", session[s].tx_connect_speed);
459 if (session[s].filter_in && session[s].filter_in <= MAXFILTER)
460 cli_print(cli, "\tFilter in:\t%u (%s)", session[s].filter_in, ip_filters[session[s].filter_in - 1].name);
461 if (session[s].filter_out && session[s].filter_out <= MAXFILTER)
462 cli_print(cli, "\tFilter out:\t%u (%s)", session[s].filter_out, ip_filters[session[s].filter_out - 1].name);
463 if (session[s].snoop_ip && session[s].snoop_port)
464 cli_print(cli, "\tIntercepted:\t%s:%d", fmtaddr(session[s].snoop_ip, 0), session[s] .snoop_port);
465 else
466 cli_print(cli, "\tIntercepted:\tno");
467
468 cli_print(cli, "\tWalled Garden:\t%s", session[s].walled_garden ? "YES" : "no");
469 {
470 int t = (session[s].throttle_in || session[s].throttle_out);
471 cli_print(cli, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
472 t ? "YES" : "no", t ? " (" : "",
473 session[s].throttle_in, session[s].throttle_in ? "kbps" : t ? "-" : "",
474 t ? "/" : "",
475 session[s].throttle_out, session[s].throttle_out ? "kbps" : t ? "-" : "",
476 t ? ")" : "");
477 }
478
479 b_in = session[s].tbf_in;
480 b_out = session[s].tbf_out;
481 if (b_in || b_out)
482 cli_print(cli, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
483 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
484 "ByteSent", "PackSent", "PackDrop", "PackDelay");
485
486 if (b_in)
487 cli_print(cli, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
488 b_in,
489 (filter_list[b_in].next ? "*" : " "),
490 (b_in < 100 ? "\t" : ""),
491 filter_list[b_in].rate * 8,
492 filter_list[b_in].credit,
493 filter_list[b_in].queued,
494 filter_list[b_in].b_queued,
495 filter_list[b_in].p_queued,
496 filter_list[b_in].b_sent,
497 filter_list[b_in].p_sent,
498 filter_list[b_in].p_dropped,
499 filter_list[b_in].p_delayed);
500
501 if (b_out)
502 cli_print(cli, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
503 b_out,
504 (filter_list[b_out].next ? "*" : " "),
505 (b_out < 100 ? "\t" : ""),
506 filter_list[b_out].rate * 8,
507 filter_list[b_out].credit,
508 filter_list[b_out].queued,
509 filter_list[b_out].b_queued,
510 filter_list[b_out].p_queued,
511 filter_list[b_out].b_sent,
512 filter_list[b_out].p_sent,
513 filter_list[b_out].p_dropped,
514 filter_list[b_out].p_delayed);
515
516 }
517 return CLI_OK;
518 }
519
520 // Show Summary
521 cli_print(cli, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-15s %s",
522 "SID",
523 "TID",
524 "Username",
525 "IP",
526 "I",
527 "T",
528 "G",
529 "6",
530 "opened",
531 "downloaded",
532 "uploaded",
533 "idle",
534 "Rem.Time",
535 "LAC",
536 "CLI");
537
538 for (i = 1; i < MAXSESSION; i++)
539 {
540 uint32_t rem_time;
541 if (!session[i].opened) continue;
542 if (session[i].bundle && bundle[session[i].bundle].num_of_links > 1)
543 rem_time = session[i].timeout ? (session[i].timeout - bundle[session[i].bundle].online_time) : 0;
544 else
545 rem_time = session[i].timeout ? (session[i].timeout - (time_now-session[i].opened)) : 0;
546
547 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %-15s %s",
548 i,
549 session[i].tunnel,
550 session[i].user[0] ? session[i].user : "*",
551 fmtaddr(htonl(session[i].ip), 0),
552 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
553 (session[i].throttle_in || session[i].throttle_out) ? "Y" : "N",
554 (session[i].walled_garden) ? "Y" : "N",
555 (session[i].ppp.ipv6cp == Opened) ? "Y" : "N",
556 abs(time_now - (unsigned long)session[i].opened),
557 (unsigned long)session[i].cout,
558 (unsigned long)session[i].cin,
559 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
560 (unsigned long)(rem_time),
561 fmtaddr(htonl(tunnel[ session[i].tunnel ].ip), 1),
562 session[i].calling[0] ? session[i].calling : "*");
563 }
564 return CLI_OK;
565 }
566
567 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
568 {
569 int i, x, show_all = 0;
570 char *states[] = {
571 "Free",
572 "Open",
573 "Closing",
574 "Opening",
575 };
576
577 if (CLI_HELP_REQUESTED)
578 {
579 if (argc > 1)
580 return cli_arg_help(cli, 1,
581 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
582 NULL);
583
584 return cli_arg_help(cli, 1,
585 "all", "Show all tunnels, including unused",
586 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
587 NULL);
588 }
589
590 time(&time_now);
591 if (argc > 0)
592 {
593 if (strcmp(argv[0], "all") == 0)
594 {
595 show_all = 1;
596 }
597 else
598 {
599 // Show individual tunnel
600 for (i = 0; i < argc; i++)
601 {
602 char s[65535] = {0};
603 unsigned int t;
604 t = atoi(argv[i]);
605 if (t <= 0 || t >= MAXTUNNEL)
606 {
607 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
608 continue;
609 }
610 cli_print(cli, "\r\nTunnel %d:", t);
611 cli_print(cli, "\tState:\t\t%s", states[tunnel[t].state]);
612 cli_print(cli, "\tHostname:\t%s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
613 cli_print(cli, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel[t].ip), 0));
614 cli_print(cli, "\tRemote Port:\t%d", tunnel[t].port);
615 cli_print(cli, "\tRx Window:\t%u", tunnel[t].window);
616 cli_print(cli, "\tNext Recv:\t%u", tunnel[t].nr);
617 cli_print(cli, "\tNext Send:\t%u", tunnel[t].ns);
618 cli_print(cli, "\tQueue Len:\t%u", tunnel[t].controlc);
619 cli_print(cli, "\tLast Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
620
621 for (x = 0; x < MAXSESSION; x++)
622 if (session[x].tunnel == t && session[x].opened && !session[x].die)
623 sprintf(s, "%s%u ", s, x);
624
625 cli_print(cli, "\tSessions:\t%s", s);
626 }
627 return CLI_OK;
628 }
629 }
630
631 // Show tunnel summary
632 cli_print(cli, "%4s %20s %20s %6s %s",
633 "TID",
634 "Hostname",
635 "IP",
636 "State",
637 "Sessions");
638
639 for (i = 1; i < MAXTUNNEL; i++)
640 {
641 int sessions = 0;
642 if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
643
644 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
645 cli_print(cli, "%4d %20s %20s %6s %6d",
646 i,
647 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
648 fmtaddr(htonl(tunnel[i].ip), 0),
649 states[tunnel[i].state],
650 sessions);
651 }
652
653 return CLI_OK;
654 }
655
656 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
657 {
658 char sid[32][8];
659 char *sargv[32];
660 int sargc = 0;
661 int i;
662
663 if (CLI_HELP_REQUESTED)
664 return cli_arg_help(cli, 1,
665 "USER", "Show details for specific username",
666 NULL);
667
668 for (i = 0; i < MAXSESSION; i++)
669 {
670 if (!session[i].opened) continue;
671 if (!session[i].user[0]) continue;
672 if (argc > 0)
673 {
674 int j;
675 for (j = 0; j < argc && sargc < 32; j++)
676 {
677 if (strcmp(argv[j], session[i].user) == 0)
678 {
679 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
680 sargv[sargc] = sid[sargc];
681 sargc++;
682 }
683 }
684
685 continue;
686 }
687
688 cli_print(cli, "%s", session[i].user);
689 }
690
691 if (sargc > 0)
692 return cmd_show_session(cli, "users", sargv, sargc);
693
694 return CLI_OK;
695 }
696
697 #ifdef STATISTICS
698 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
699 {
700 if (CLI_HELP_REQUESTED)
701 return CLI_HELP_NO_ARGS;
702
703 cli_print(cli, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
704 cli_print(cli, "%-10s %10u %10u %10u %10u", "RX",
705 GET_STAT(tun_rx_bytes),
706 GET_STAT(tun_rx_packets),
707 GET_STAT(tun_rx_errors),
708 GET_STAT(tun_rx_dropped));
709 cli_print(cli, "%-10s %10u %10u %10u", "TX",
710 GET_STAT(tun_tx_bytes),
711 GET_STAT(tun_tx_packets),
712 GET_STAT(tun_tx_errors));
713 cli_print(cli, "");
714
715 cli_print(cli, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
716 cli_print(cli, "%-10s %10u %10u %10u", "RX",
717 GET_STAT(tunnel_rx_bytes),
718 GET_STAT(tunnel_rx_packets),
719 GET_STAT(tunnel_rx_errors));
720 cli_print(cli, "%-10s %10u %10u %10u %10u", "TX",
721 GET_STAT(tunnel_tx_bytes),
722 GET_STAT(tunnel_tx_packets),
723 GET_STAT(tunnel_tx_errors),
724 GET_STAT(tunnel_retries));
725 cli_print(cli, "");
726
727 cli_print(cli, "%-30s%-10s", "Counter", "Value");
728 cli_print(cli, "-----------------------------------------");
729 cli_print(cli, "%-30s%u", "radius_retries", GET_STAT(radius_retries));
730 cli_print(cli, "%-30s%u", "arp_sent", GET_STAT(arp_sent));
731 cli_print(cli, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped));
732 cli_print(cli, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created));
733 cli_print(cli, "%-30s%u", "session_created", GET_STAT(session_created));
734 cli_print(cli, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout));
735 cli_print(cli, "%-30s%u", "session_timeout", GET_STAT(session_timeout));
736 cli_print(cli, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout));
737 cli_print(cli, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow));
738 cli_print(cli, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow));
739 cli_print(cli, "%-30s%u", "session_overflow", GET_STAT(session_overflow));
740 cli_print(cli, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated));
741 cli_print(cli, "%-30s%u", "ip_freed", GET_STAT(ip_freed));
742 cli_print(cli, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded));
743 cli_print(cli, "%-30s%u", "recv_forward", GET_STAT(recv_forward));
744 cli_print(cli, "%-30s%u", "select_called", GET_STAT(select_called));
745 cli_print(cli, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used));
746 cli_print(cli, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded));
747
748
749 #ifdef STAT_CALLS
750 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
751 cli_print(cli, "-----------------------------------------");
752 cli_print(cli, "%-30s%u", "call_processtun", GET_STAT(call_processtun));
753 cli_print(cli, "%-30s%u", "call_processipout", GET_STAT(call_processipout));
754 cli_print(cli, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out));
755 cli_print(cli, "%-30s%u", "call_processudp", GET_STAT(call_processudp));
756 cli_print(cli, "%-30s%u", "call_processpap", GET_STAT(call_processpap));
757 cli_print(cli, "%-30s%u", "call_processchap", GET_STAT(call_processchap));
758 cli_print(cli, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp));
759 cli_print(cli, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp));
760 cli_print(cli, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp));
761 cli_print(cli, "%-30s%u", "call_processipin", GET_STAT(call_processipin));
762 cli_print(cli, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in));
763 cli_print(cli, "%-30s%u", "call_processccp", GET_STAT(call_processccp));
764 cli_print(cli, "%-30s%u", "call_processrad", GET_STAT(call_processrad));
765 cli_print(cli, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp));
766 cli_print(cli, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp));
767 cli_print(cli, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap));
768 cli_print(cli, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip));
769 cli_print(cli, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6));
770 cli_print(cli, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
771 cli_print(cli, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend));
772 cli_print(cli, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill));
773 cli_print(cli, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
774 cli_print(cli, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill));
775 cli_print(cli, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
776 cli_print(cli, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup));
777 cli_print(cli, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address));
778 cli_print(cli, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address));
779 cli_print(cli, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
780 cli_print(cli, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend));
781 cli_print(cli, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry));
782 cli_print(cli, "%-30s%u", "call_random_data", GET_STAT(call_random_data));
783 #endif /* STAT_CALLS */
784
785 {
786 time_t l = GET_STAT(last_reset);
787 char *t = ctime(&l);
788 char *p = strchr(t, '\n');
789 if (p) *p = 0;
790
791 cli_print(cli, "");
792 cli_print(cli, "Last counter reset %s", t);
793 }
794
795 return CLI_OK;
796 }
797
798 static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
799 {
800 if (CLI_HELP_REQUESTED)
801 return CLI_HELP_NO_ARGS;
802
803 memset(_statistics, 0, sizeof(struct Tstats));
804 SET_STAT(last_reset, time(NULL));
805
806 cli_print(cli, "Counters cleared");
807 return CLI_OK;
808 }
809 #endif /* STATISTICS */
810
811 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
812 {
813 int tag = 0;
814 int file = 0;
815 int i = 0;
816
817 if (CLI_HELP_REQUESTED)
818 return cli_arg_help(cli, 1,
819 "tag", "Include CVS release tag",
820 "file", "Include file versions",
821 NULL);
822
823 for (i = 0; i < argc; i++)
824 if (!strcmp(argv[i], "tag"))
825 tag++;
826 else if (!strcmp(argv[i], "file"))
827 file++;
828
829 cli_print(cli, "L2TPNS %s", VERSION);
830 if (tag)
831 {
832 char const *p = strchr(cvs_name, ':');
833 char const *e;
834 if (p)
835 {
836 p++;
837 while (isspace(*p))
838 p++;
839 }
840
841 if (!p || *p == '$')
842 p = "HEAD";
843
844 e = strpbrk(p, " \t$");
845 cli_print(cli, "Tag: %.*s", (int) (e ? e - p + 1 : strlen(p)), p);
846 }
847
848 if (file)
849 {
850 extern linked_list *loaded_plugins;
851 void *p;
852
853 cli_print(cli, "Files:");
854 cli_print(cli, " %s", cvs_id_arp);
855 #ifdef BGP
856 cli_print(cli, " %s", cvs_id_bgp);
857 #endif /* BGP */
858 cli_print(cli, " %s", cvs_id_cli);
859 cli_print(cli, " %s", cvs_id_cluster);
860 cli_print(cli, " %s", cvs_id_constants);
861 cli_print(cli, " %s", cvs_id_control);
862 cli_print(cli, " %s", cvs_id_icmp);
863 cli_print(cli, " %s", cvs_id_l2tpns);
864 cli_print(cli, " %s", cvs_id_ll);
865 cli_print(cli, " %s", cvs_id_ppp);
866 cli_print(cli, " %s", cvs_id_radius);
867 cli_print(cli, " %s", cvs_id_tbf);
868 cli_print(cli, " %s", cvs_id_util);
869
870 ll_reset(loaded_plugins);
871 while ((p = ll_next(loaded_plugins)))
872 {
873 char const **id = dlsym(p, "cvs_id");
874 if (id)
875 cli_print(cli, " %s", *id);
876 }
877 }
878
879 return CLI_OK;
880 }
881
882 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
883 {
884 int i;
885 int used = 0, free = 0, show_all = 0;
886
887 if (!config->cluster_iam_master)
888 {
889 cli_print(cli, "Can't do this on a slave. Do it on %s",
890 fmtaddr(config->cluster_master_address, 0));
891
892 return CLI_OK;
893 }
894
895 if (CLI_HELP_REQUESTED)
896 {
897 if (argc > 1)
898 return cli_arg_help(cli, 1, NULL);
899
900 return cli_arg_help(cli, 1,
901 "all", "Show all pool addresses, including unused",
902 NULL);
903 }
904
905 if (argc > 0 && strcmp(argv[0], "all") == 0)
906 show_all = 1;
907
908 time(&time_now);
909 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
910 for (i = 0; i < MAXIPPOOL; i++)
911 {
912 if (!ip_address_pool[i].address) continue;
913 if (ip_address_pool[i].assigned)
914 {
915 cli_print(cli, "%-15s\tY %8d %s",
916 fmtaddr(htonl(ip_address_pool[i].address), 0),
917 ip_address_pool[i].session,
918 session[ip_address_pool[i].session].user);
919
920 used++;
921 }
922 else
923 {
924 if (ip_address_pool[i].last)
925 cli_print(cli, "%-15s\tN %8s [%s] %ds",
926 fmtaddr(htonl(ip_address_pool[i].address), 0), "",
927 ip_address_pool[i].user, (int) time_now - ip_address_pool[i].last);
928
929 else if (show_all)
930 cli_print(cli, "%-15s\tN", fmtaddr(htonl(ip_address_pool[i].address), 0));
931
932 free++;
933 }
934 }
935
936 if (!show_all)
937 cli_print(cli, "(Not displaying unused addresses)");
938
939 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
940 return CLI_OK;
941 }
942
943 static FILE *save_config_fh = 0;
944 static void print_save_config(struct cli_def *cli, char *string)
945 {
946 if (save_config_fh)
947 fprintf(save_config_fh, "%s\n", string);
948 }
949
950 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
951 {
952 if (CLI_HELP_REQUESTED)
953 return CLI_HELP_NO_ARGS;
954
955 if ((save_config_fh = fopen(config->config_file, "w")))
956 {
957 cli_print(cli, "Writing configuration");
958 cli_print_callback(cli, print_save_config);
959 cmd_show_run(cli, command, argv, argc);
960 cli_print_callback(cli, NULL);
961 fclose(save_config_fh);
962 save_config_fh = 0;
963 }
964 else
965 {
966 cli_error(cli, "Error writing configuration: %s", strerror(errno));
967 }
968 return CLI_OK;
969 }
970
971 static char const *show_access_list_rule(int extended, ip_filter_rulet *rule);
972
973 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
974 {
975 int i;
976 char ipv6addr[INET6_ADDRSTRLEN];
977
978 if (CLI_HELP_REQUESTED)
979 return CLI_HELP_NO_ARGS;
980
981 cli_print(cli, "# Current configuration:");
982
983 for (i = 0; config_values[i].key; i++)
984 {
985 void *value = ((void *)config) + config_values[i].offset;
986 if (config_values[i].type == STRING)
987 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *) value);
988 else if (config_values[i].type == IPv4)
989 cli_print(cli, "set %s %s", config_values[i].key, fmtaddr(*(in_addr_t *) value, 0));
990 else if (config_values[i].type == IPv6)
991 cli_print(cli, "set %s %s", config_values[i].key, inet_ntop(AF_INET6, value, ipv6addr, INET6_ADDRSTRLEN));
992 else if (config_values[i].type == SHORT)
993 cli_print(cli, "set %s %hu", config_values[i].key, *(short *) value);
994 else if (config_values[i].type == BOOL)
995 cli_print(cli, "set %s %s", config_values[i].key, (*(int *) value) ? "yes" : "no");
996 else if (config_values[i].type == INT)
997 cli_print(cli, "set %s %d", config_values[i].key, *(int *) value);
998 else if (config_values[i].type == UNSIGNED_LONG)
999 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *) value);
1000 }
1001
1002 cli_print(cli, "# Plugins");
1003 for (i = 0; i < MAXPLUGINS; i++)
1004 {
1005 if (*config->plugins[i])
1006 {
1007 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
1008 }
1009 }
1010
1011 #ifdef BGP
1012 if (config->as_number)
1013 {
1014 int k;
1015 int h;
1016
1017 cli_print(cli, "# BGP");
1018 cli_print(cli, "router bgp %u", config->as_number);
1019 for (i = 0; i < BGP_NUM_PEERS; i++)
1020 {
1021 if (!config->neighbour[i].name[0])
1022 continue;
1023
1024 cli_print(cli, " neighbour %s remote-as %u", config->neighbour[i].name, config->neighbour[i].as);
1025
1026 k = config->neighbour[i].keepalive;
1027 h = config->neighbour[i].hold;
1028
1029 if (k == -1)
1030 {
1031 if (h == -1)
1032 continue;
1033
1034 k = BGP_KEEPALIVE_TIME;
1035 }
1036
1037 if (h == -1)
1038 h = BGP_HOLD_TIME;
1039
1040 cli_print(cli, " neighbour %s timers %d %d", config->neighbour[i].name, k, h);
1041 }
1042 }
1043 #endif
1044
1045 cli_print(cli, "# Filters");
1046 for (i = 0; i < MAXFILTER; i++)
1047 {
1048 ip_filter_rulet *rules;
1049 if (!*ip_filters[i].name)
1050 continue;
1051
1052 cli_print(cli, "ip access-list %s %s",
1053 ip_filters[i].extended ? "extended" : "standard",
1054 ip_filters[i].name);
1055
1056 rules = ip_filters[i].rules;
1057 while (rules->action)
1058 cli_print(cli, "%s", show_access_list_rule(ip_filters[i].extended, rules++));
1059 }
1060
1061 cli_print(cli, "# end");
1062 return CLI_OK;
1063 }
1064
1065 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
1066 {
1067 int i, free = 0, used = 0, show_all = 0;
1068 char *states[] = {
1069 "NULL",
1070 "CHAP",
1071 "AUTH",
1072 "IPCP",
1073 "START",
1074 "STOP",
1075 "INTRM",
1076 "WAIT",
1077 };
1078
1079 if (CLI_HELP_REQUESTED)
1080 {
1081 if (argc > 1)
1082 return cli_arg_help(cli, 1, NULL);
1083
1084 return cli_arg_help(cli, 1,
1085 "all", "Show all RADIUS sessions, including unused",
1086 NULL);
1087 }
1088
1089 cli_print(cli, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1090
1091 time(&time_now);
1092
1093 if (argc > 0 && strcmp(argv[0], "all") == 0)
1094 show_all = 1;
1095
1096 for (i = 1; i < MAXRADIUS; i++)
1097 {
1098 if (radius[i].state == RADIUSNULL)
1099 free++;
1100 else
1101 used++;
1102
1103 if (!show_all && radius[i].state == RADIUSNULL) continue;
1104
1105 cli_print(cli, "%6d%7d%5d%6s%9d%9u%4d",
1106 i,
1107 i >> RADIUS_SHIFT,
1108 i & RADIUS_MASK,
1109 states[radius[i].state],
1110 radius[i].session,
1111 radius[i].retry,
1112 radius[i].try);
1113 }
1114
1115 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
1116
1117 return CLI_OK;
1118 }
1119
1120 static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
1121 {
1122 int i;
1123
1124 if (CLI_HELP_REQUESTED)
1125 return CLI_HELP_NO_ARGS;
1126
1127 cli_print(cli, "Plugins currently loaded:");
1128 for (i = 0; i < MAXPLUGINS; i++)
1129 if (*config->plugins[i])
1130 cli_print(cli, " %s", config->plugins[i]);
1131
1132 return CLI_OK;
1133 }
1134
1135 static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1136 {
1137 int i;
1138
1139 if (CLI_HELP_REQUESTED)
1140 return CLI_HELP_NO_ARGS;
1141
1142 cli_print(cli, "%5s %4s %-32s %7s %6s %6s %6s",
1143 "SID",
1144 "TID",
1145 "Username",
1146 "Rate In",
1147 "Out",
1148 "TBFI",
1149 "TBFO");
1150
1151 for (i = 0; i < MAXSESSION; i++)
1152 {
1153 if (session[i].throttle_in || session[i].throttle_out)
1154 cli_print(cli, "%5d %4d %-32s %6d %6d %6d %6d",
1155 i,
1156 session[i].tunnel,
1157 session[i].user,
1158 session[i].throttle_in,
1159 session[i].throttle_out,
1160 session[i].tbf_in,
1161 session[i].tbf_out);
1162 }
1163
1164 return CLI_OK;
1165 }
1166
1167 static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
1168 {
1169 if (CLI_HELP_REQUESTED)
1170 return CLI_HELP_NO_ARGS;
1171
1172 cli_print(cli, " _\n"
1173 "//\\\n"
1174 "V \\\n"
1175 " \\ \\_\n"
1176 " \\,'.`-.\n"
1177 " |\\ `. `.\n"
1178 " ( \\ `. `-. _,.-:\\\n"
1179 " \\ \\ `. `-._ __..--' ,-';/\n"
1180 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1181 " `. `. `-._ __..--' ,' /\n"
1182 " `. `-_ ``--..'' _.-' ,'\n"
1183 " `-_ `-.___ __,--' ,'\n"
1184 " `-.__ `----\"\"\" __.-'\n"
1185 "hh `--..____..--'");
1186
1187 return CLI_OK;
1188 }
1189
1190 static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
1191 {
1192 int i;
1193 sessionidt s;
1194
1195 if (CLI_HELP_REQUESTED)
1196 return cli_arg_help(cli, argc > 1,
1197 "USER", "Username of session to drop", NULL);
1198
1199 if (!config->cluster_iam_master)
1200 {
1201 cli_error(cli, "Can't do this on a slave. Do it on %s",
1202 fmtaddr(config->cluster_master_address, 0));
1203
1204 return CLI_OK;
1205 }
1206
1207 if (!argc)
1208 {
1209 cli_error(cli, "Specify a user to drop");
1210 return CLI_OK;
1211 }
1212
1213 for (i = 0; i < argc; i++)
1214 {
1215 if (!(s = sessionbyuser(argv[i])))
1216 {
1217 cli_error(cli, "User %s is not connected", argv[i]);
1218 continue;
1219 }
1220
1221 if (session[s].ip && session[s].opened && !session[s].die)
1222 {
1223 cli_print(cli, "Dropping user %s", session[s].user);
1224 cli_session_actions[s].action |= CLI_SESS_KILL;
1225 }
1226 }
1227
1228 return CLI_OK;
1229 }
1230
1231 static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1232 {
1233 int i;
1234 tunnelidt t;
1235
1236 if (CLI_HELP_REQUESTED)
1237 return cli_arg_help(cli, argc > 1,
1238 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1239
1240 if (!config->cluster_iam_master)
1241 {
1242 cli_error(cli, "Can't do this on a slave. Do it on %s",
1243 fmtaddr(config->cluster_master_address, 0));
1244
1245 return CLI_OK;
1246 }
1247
1248 if (!argc)
1249 {
1250 cli_error(cli, "Specify a tunnel to drop");
1251 return CLI_OK;
1252 }
1253
1254 for (i = 0; i < argc; i++)
1255 {
1256 if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL))
1257 {
1258 cli_error(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1259 continue;
1260 }
1261
1262 if (!tunnel[t].ip)
1263 {
1264 cli_error(cli, "Tunnel %d is not connected", t);
1265 continue;
1266 }
1267
1268 if (tunnel[t].die)
1269 {
1270 cli_error(cli, "Tunnel %d is already being shut down", t);
1271 continue;
1272 }
1273
1274 cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname);
1275 cli_tunnel_actions[t].action |= CLI_TUN_KILL;
1276 }
1277
1278 return CLI_OK;
1279 }
1280
1281 static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1282 {
1283 int i;
1284 sessionidt s;
1285
1286 if (CLI_HELP_REQUESTED)
1287 return cli_arg_help(cli, argc > 1,
1288 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1289
1290 if (!config->cluster_iam_master)
1291 {
1292 cli_error(cli, "Can't do this on a slave. Do it on %s",
1293 fmtaddr(config->cluster_master_address, 0));
1294
1295 return CLI_OK;
1296 }
1297
1298 if (!argc)
1299 {
1300 cli_error(cli, "Specify a session id to drop");
1301 return CLI_OK;
1302 }
1303
1304 for (i = 0; i < argc; i++)
1305 {
1306 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1307 {
1308 cli_error(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1309 continue;
1310 }
1311
1312 if (session[s].ip && session[s].opened && !session[s].die)
1313 {
1314 cli_print(cli, "Dropping session %d", s);
1315 cli_session_actions[s].action |= CLI_SESS_KILL;
1316 }
1317 else
1318 {
1319 cli_error(cli, "Session %d is not active.", s);
1320 }
1321 }
1322
1323 return CLI_OK;
1324 }
1325
1326 static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1327 {
1328 in_addr_t ip;
1329 uint16_t port;
1330 sessionidt s;
1331
1332 if (CLI_HELP_REQUESTED)
1333 {
1334 switch (argc)
1335 {
1336 case 1:
1337 return cli_arg_help(cli, 0,
1338 "USER", "Username of session to snoop", NULL);
1339
1340 case 2:
1341 return cli_arg_help(cli, 0,
1342 "A.B.C.D", "IP address of snoop destination", NULL);
1343
1344 case 3:
1345 return cli_arg_help(cli, 0,
1346 "N", "Port of snoop destination", NULL);
1347
1348 case 4:
1349 if (!argv[3][1])
1350 return cli_arg_help(cli, 1, NULL);
1351
1352 default:
1353 return CLI_OK;
1354 }
1355 }
1356
1357 if (!config->cluster_iam_master)
1358 {
1359 cli_error(cli, "Can't do this on a slave. Do it on %s",
1360 fmtaddr(config->cluster_master_address, 0));
1361
1362 return CLI_OK;
1363 }
1364
1365 if (argc < 3)
1366 {
1367 cli_error(cli, "Specify username, ip and port");
1368 return CLI_OK;
1369 }
1370
1371 if (!(s = sessionbyuser(argv[0])))
1372 {
1373 cli_error(cli, "User %s is not connected", argv[0]);
1374 return CLI_OK;
1375 }
1376
1377 ip = inet_addr(argv[1]);
1378 if (!ip || ip == INADDR_NONE)
1379 {
1380 cli_error(cli, "Cannot parse IP \"%s\"", argv[1]);
1381 return CLI_OK;
1382 }
1383
1384 port = atoi(argv[2]);
1385 if (!port)
1386 {
1387 cli_error(cli, "Invalid port %s", argv[2]);
1388 return CLI_OK;
1389 }
1390
1391 cli_print(cli, "Snooping user %s to %s:%d", argv[0], fmtaddr(ip, 0), port);
1392 cli_session_actions[s].snoop_ip = ip;
1393 cli_session_actions[s].snoop_port = port;
1394 cli_session_actions[s].action |= CLI_SESS_SNOOP;
1395
1396 return CLI_OK;
1397 }
1398
1399 static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1400 {
1401 int i;
1402 sessionidt s;
1403
1404 if (CLI_HELP_REQUESTED)
1405 return cli_arg_help(cli, argc > 1,
1406 "USER", "Username of session to unsnoop", NULL);
1407
1408 if (!config->cluster_iam_master)
1409 {
1410 cli_error(cli, "Can't do this on a slave. Do it on %s",
1411 fmtaddr(config->cluster_master_address, 0));
1412
1413 return CLI_OK;
1414 }
1415
1416 if (!argc)
1417 {
1418 cli_error(cli, "Specify a user to unsnoop");
1419 return CLI_OK;
1420 }
1421
1422 for (i = 0; i < argc; i++)
1423 {
1424 if (!(s = sessionbyuser(argv[i])))
1425 {
1426 cli_error(cli, "User %s is not connected", argv[i]);
1427 continue;
1428 }
1429
1430 cli_print(cli, "Not snooping user %s", argv[i]);
1431 cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
1432 }
1433
1434 return CLI_OK;
1435 }
1436
1437 static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1438 {
1439 int rate_in = 0;
1440 int rate_out = 0;
1441 sessionidt s;
1442
1443 /*
1444 throttle USER - throttle in/out to default rate
1445 throttle USER RATE - throttle in/out to default rate
1446 throttle USER in RATE - throttle input only
1447 throttle USER out RATE - throttle output only
1448 throttle USER in RATE out RATE - throttle both
1449 */
1450
1451 if (CLI_HELP_REQUESTED)
1452 {
1453 switch (argc)
1454 {
1455 case 1:
1456 return cli_arg_help(cli, 0,
1457 "USER", "Username of session to throttle", NULL);
1458
1459 case 2:
1460 return cli_arg_help(cli, 1,
1461 "RATE", "Rate in kbps (in and out)",
1462 "in", "Select incoming rate",
1463 "out", "Select outgoing rate", NULL);
1464
1465 case 4:
1466 return cli_arg_help(cli, 1,
1467 "in", "Select incoming rate",
1468 "out", "Select outgoing rate", NULL);
1469
1470 case 3:
1471 if (isdigit(argv[1][0]))
1472 return cli_arg_help(cli, 1, NULL);
1473
1474 case 5:
1475 return cli_arg_help(cli, 0, "RATE", "Rate in kbps", NULL);
1476
1477 default:
1478 return cli_arg_help(cli, argc > 1, NULL);
1479 }
1480 }
1481
1482 if (!config->cluster_iam_master)
1483 {
1484 cli_error(cli, "Can't do this on a slave. Do it on %s",
1485 fmtaddr(config->cluster_master_address, 0));
1486
1487 return CLI_OK;
1488 }
1489
1490 if (argc == 0)
1491 {
1492 cli_error(cli, "Specify a user to throttle");
1493 return CLI_OK;
1494 }
1495
1496 if (!(s = sessionbyuser(argv[0])))
1497 {
1498 cli_error(cli, "User %s is not connected", argv[0]);
1499 return CLI_OK;
1500 }
1501
1502 if (argc == 1)
1503 {
1504 rate_in = rate_out = config->rl_rate;
1505 }
1506 else if (argc == 2)
1507 {
1508 rate_in = rate_out = atoi(argv[1]);
1509 if (rate_in < 1)
1510 {
1511 cli_error(cli, "Invalid rate \"%s\"", argv[1]);
1512 return CLI_OK;
1513 }
1514 }
1515 else if (argc == 3 || argc == 5)
1516 {
1517 int i;
1518 for (i = 1; i < argc - 1; i += 2)
1519 {
1520 int r = 0;
1521 if (MATCH("in", argv[i]))
1522 r = rate_in = atoi(argv[i+1]);
1523 else if (MATCH("out", argv[i]))
1524 r = rate_out = atoi(argv[i+1]);
1525
1526 if (r < 1)
1527 {
1528 cli_error(cli, "Invalid rate specification \"%s %s\"", argv[i], argv[i+1]);
1529 return CLI_OK;
1530 }
1531 }
1532 }
1533 else
1534 {
1535 cli_error(cli, "Invalid arguments");
1536 return CLI_OK;
1537 }
1538
1539 if ((rate_in && session[s].throttle_in) || (rate_out && session[s].throttle_out))
1540 {
1541 cli_error(cli, "User %s already throttled, unthrottle first", argv[0]);
1542 return CLI_OK;
1543 }
1544
1545 cli_session_actions[s].throttle_in = cli_session_actions[s].throttle_out = -1;
1546 if (rate_in && session[s].throttle_in != rate_in)
1547 cli_session_actions[s].throttle_in = rate_in;
1548
1549 if (rate_out && session[s].throttle_out != rate_out)
1550 cli_session_actions[s].throttle_out = rate_out;
1551
1552 if (cli_session_actions[s].throttle_in == -1 &&
1553 cli_session_actions[s].throttle_out == -1)
1554 {
1555 cli_error(cli, "User %s already throttled at this rate", argv[0]);
1556 return CLI_OK;
1557 }
1558
1559 cli_print(cli, "Throttling user %s", argv[0]);
1560 cli_session_actions[s].action |= CLI_SESS_THROTTLE;
1561
1562 return CLI_OK;
1563 }
1564
1565 static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1566 {
1567 int i;
1568 sessionidt s;
1569
1570 if (CLI_HELP_REQUESTED)
1571 return cli_arg_help(cli, argc > 1,
1572 "USER", "Username of session to unthrottle", NULL);
1573
1574 if (!config->cluster_iam_master)
1575 {
1576 cli_error(cli, "Can't do this on a slave. Do it on %s",
1577 fmtaddr(config->cluster_master_address, 0));
1578
1579 return CLI_OK;
1580 }
1581
1582 if (!argc)
1583 {
1584 cli_error(cli, "Specify a user to unthrottle");
1585 return CLI_OK;
1586 }
1587
1588 for (i = 0; i < argc; i++)
1589 {
1590 if (!(s = sessionbyuser(argv[i])))
1591 {
1592 cli_error(cli, "User %s is not connected", argv[i]);
1593 continue;
1594 }
1595
1596 if (session[s].throttle_in || session[s].throttle_out)
1597 {
1598 cli_print(cli, "Unthrottling user %s", argv[i]);
1599 cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
1600 }
1601 else
1602 {
1603 cli_error(cli, "User %s not throttled", argv[i]);
1604 }
1605 }
1606
1607 return CLI_OK;
1608 }
1609
1610 static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1611 {
1612 int i;
1613
1614 if (CLI_HELP_REQUESTED)
1615 return cli_arg_help(cli, 1,
1616 "all", "Enable debugging for all except \"data\"",
1617 "critical", "", // FIXME: add descriptions
1618 "error", "",
1619 "warning", "",
1620 "info", "",
1621 "calls", "",
1622 "data", "",
1623 NULL);
1624
1625 if (!argc)
1626 {
1627 char *p = (char *) &debug_flags;
1628 for (i = 0; i < sizeof(debug_flags); i++)
1629 {
1630 if (p[i])
1631 {
1632 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1633 (debug_flags.critical) ? " critical" : "",
1634 (debug_flags.error) ? " error" : "",
1635 (debug_flags.warning) ? " warning" : "",
1636 (debug_flags.info) ? " info" : "",
1637 (debug_flags.calls) ? " calls" : "",
1638 (debug_flags.data) ? " data" : "");
1639
1640 return CLI_OK;
1641 }
1642 }
1643
1644 cli_print(cli, "Debugging off");
1645 return CLI_OK;
1646 }
1647
1648 for (i = 0; i < argc; i++)
1649 {
1650 int len = strlen(argv[i]);
1651
1652 if (argv[i][0] == 'c' && len < 2)
1653 len = 2; /* distinguish [cr]itical from [ca]lls */
1654
1655 if (!strncmp("critical", argv[i], len)) { debug_flags.critical = 1; continue; }
1656 if (!strncmp("error", argv[i], len)) { debug_flags.error = 1; continue; }
1657 if (!strncmp("warning", argv[i], len)) { debug_flags.warning = 1; continue; }
1658 if (!strncmp("info", argv[i], len)) { debug_flags.info = 1; continue; }
1659 if (!strncmp("calls", argv[i], len)) { debug_flags.calls = 1; continue; }
1660 if (!strncmp("data", argv[i], len)) { debug_flags.data = 1; continue; }
1661 if (!strncmp("all", argv[i], len))
1662 {
1663 memset(&debug_flags, 1, sizeof(debug_flags));
1664 debug_flags.data = 0;
1665 continue;
1666 }
1667
1668 cli_error(cli, "Invalid debugging flag \"%s\"", argv[i]);
1669 }
1670
1671 return CLI_OK;
1672 }
1673
1674 static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1675 {
1676 int i;
1677
1678 if (CLI_HELP_REQUESTED)
1679 return cli_arg_help(cli, 1,
1680 "all", "Disable all debugging",
1681 "critical", "", // FIXME: add descriptions
1682 "error", "",
1683 "warning", "",
1684 "info", "",
1685 "calls", "",
1686 "data", "",
1687 NULL);
1688
1689 if (!argc)
1690 {
1691 memset(&debug_flags, 0, sizeof(debug_flags));
1692 return CLI_OK;
1693 }
1694
1695 for (i = 0; i < argc; i++)
1696 {
1697 int len = strlen(argv[i]);
1698
1699 if (argv[i][0] == 'c' && len < 2)
1700 len = 2; /* distinguish [cr]itical from [ca]lls */
1701
1702 if (!strncmp("critical", argv[i], len)) { debug_flags.critical = 0; continue; }
1703 if (!strncmp("error", argv[i], len)) { debug_flags.error = 0; continue; }
1704 if (!strncmp("warning", argv[i], len)) { debug_flags.warning = 0; continue; }
1705 if (!strncmp("info", argv[i], len)) { debug_flags.info = 0; continue; }
1706 if (!strncmp("calls", argv[i], len)) { debug_flags.calls = 0; continue; }
1707 if (!strncmp("data", argv[i], len)) { debug_flags.data = 0; continue; }
1708 if (!strncmp("all", argv[i], len))
1709 {
1710 memset(&debug_flags, 0, sizeof(debug_flags));
1711 continue;
1712 }
1713
1714 cli_error(cli, "Invalid debugging flag \"%s\"", argv[i]);
1715 }
1716
1717 return CLI_OK;
1718 }
1719
1720 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1721 {
1722 int i, firstfree = 0;
1723
1724 if (CLI_HELP_REQUESTED)
1725 return cli_arg_help(cli, argc > 1,
1726 "PLUGIN", "Name of plugin to load", NULL);
1727
1728 if (argc != 1)
1729 {
1730 cli_error(cli, "Specify a plugin to load");
1731 return CLI_OK;
1732 }
1733
1734 for (i = 0; i < MAXPLUGINS; i++)
1735 {
1736 if (!*config->plugins[i] && !firstfree)
1737 firstfree = i;
1738 if (strcmp(config->plugins[i], argv[0]) == 0)
1739 {
1740 cli_error(cli, "Plugin is already loaded");
1741 return CLI_OK;
1742 }
1743 }
1744
1745 if (firstfree)
1746 {
1747 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1748 config->reload_config = 1;
1749 cli_print(cli, "Loading plugin %s", argv[0]);
1750 }
1751
1752 return CLI_OK;
1753 }
1754
1755 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1756 {
1757 int i;
1758
1759 if (CLI_HELP_REQUESTED)
1760 return cli_arg_help(cli, argc > 1,
1761 "PLUGIN", "Name of plugin to unload", NULL);
1762
1763 if (argc != 1)
1764 {
1765 cli_error(cli, "Specify a plugin to remove");
1766 return CLI_OK;
1767 }
1768
1769 for (i = 0; i < MAXPLUGINS; i++)
1770 {
1771 if (strcmp(config->plugins[i], argv[0]) == 0)
1772 {
1773 config->reload_config = 1;
1774 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1775 return CLI_OK;
1776 }
1777 }
1778
1779 cli_error(cli, "Plugin is not loaded");
1780 return CLI_OK;
1781 }
1782
1783 static char *duration(time_t secs)
1784 {
1785 static char *buf = NULL;
1786 int p = 0;
1787
1788 if (!buf) buf = calloc(64, 1);
1789
1790 if (secs >= 86400)
1791 {
1792 int days = secs / 86400;
1793 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1794 secs %= 86400;
1795 }
1796
1797 if (secs >= 3600)
1798 {
1799 int mins = secs / 60;
1800 int hrs = mins / 60;
1801
1802 mins %= 60;
1803 sprintf(buf + p, "%d:%02d", hrs, mins);
1804 }
1805 else if (secs >= 60)
1806 {
1807 int mins = secs / 60;
1808 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1809 }
1810 else
1811 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1812
1813 return buf;
1814 }
1815
1816 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1817 {
1818 FILE *fh;
1819 char buf[100], *p = buf, *loads[3];
1820 int i, num_sessions = 0;
1821
1822 if (CLI_HELP_REQUESTED)
1823 return CLI_HELP_NO_ARGS;
1824
1825 fh = fopen("/proc/loadavg", "r");
1826 fgets(buf, 100, fh);
1827 fclose(fh);
1828
1829 for (i = 0; i < 3; i++)
1830 loads[i] = strdup(strsep(&p, " "));
1831
1832 time(&time_now);
1833 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1834
1835 for (i = 1; i < MAXSESSION; i++)
1836 if (session[i].opened) num_sessions++;
1837
1838 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1839 buf,
1840 duration(time_now - config->start_time),
1841 num_sessions,
1842 loads[0], loads[1], loads[2]
1843 );
1844 for (i = 0; i < 3; i++)
1845 if (loads[i]) free(loads[i]);
1846
1847 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1848
1849 return CLI_OK;
1850 }
1851
1852 static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1853 {
1854 int i;
1855
1856 if (CLI_HELP_REQUESTED)
1857 {
1858 switch (argc)
1859 {
1860 case 1:
1861 {
1862 int len = strlen(argv[0])-1;
1863 for (i = 0; config_values[i].key; i++)
1864 if (!len || !strncmp(config_values[i].key, argv[0], len))
1865 cli_error(cli, " %s", config_values[i].key);
1866 }
1867
1868 return CLI_OK;
1869
1870 case 2:
1871 return cli_arg_help(cli, 0,
1872 "VALUE", "Value for variable", NULL);
1873
1874 case 3:
1875 if (!argv[2][1])
1876 return cli_arg_help(cli, 1, NULL);
1877
1878 default:
1879 return CLI_OK;
1880 }
1881 }
1882
1883 if (argc != 2)
1884 {
1885 cli_error(cli, "Specify variable and value");
1886 return CLI_OK;
1887 }
1888
1889 for (i = 0; config_values[i].key; i++)
1890 {
1891 void *value = ((void *) config) + config_values[i].offset;
1892 if (strcmp(config_values[i].key, argv[0]) == 0)
1893 {
1894 // Found a value to set
1895 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1896 switch (config_values[i].type)
1897 {
1898 case STRING:
1899 snprintf((char *) value, config_values[i].size, "%s", argv[1]);
1900 break;
1901 case INT:
1902 *(int *) value = atoi(argv[1]);
1903 break;
1904 case UNSIGNED_LONG:
1905 *(unsigned long *) value = atol(argv[1]);
1906 break;
1907 case SHORT:
1908 *(short *) value = atoi(argv[1]);
1909 break;
1910 case IPv4:
1911 *(in_addr_t *) value = inet_addr(argv[1]);
1912 break;
1913 case IPv6:
1914 inet_pton(AF_INET6, argv[1], value);
1915 break;
1916 case BOOL:
1917 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1918 *(int *) value = 1;
1919 else
1920 *(int *) value = 0;
1921 break;
1922 default:
1923 cli_error(cli, "Unknown variable type");
1924 break;
1925 }
1926 config->reload_config = 1;
1927 return CLI_OK;
1928 }
1929 }
1930
1931 cli_error(cli, "Unknown variable \"%s\"", argv[0]);
1932 return CLI_OK;
1933 }
1934
1935 int regular_stuff(struct cli_def *cli)
1936 {
1937 #ifdef RINGBUFFER
1938 int out = 0;
1939 int i;
1940
1941 for (i = debug_rb_tail; i != ringbuffer->tail; i = (i + 1) % RINGBUFFER_SIZE)
1942 {
1943 char *m = ringbuffer->buffer[i].message;
1944 char *p;
1945 int show = 0;
1946
1947 if (!*m) continue;
1948
1949 switch (ringbuffer->buffer[i].level)
1950 {
1951 case 0: show = debug_flags.critical; break;
1952 case 1: show = debug_flags.error; break;
1953 case 2: show = debug_flags.warning; break;
1954 case 3: show = debug_flags.info; break;
1955 case 4: show = debug_flags.calls; break;
1956 case 5: show = debug_flags.data; break;
1957 }
1958
1959 if (!show) continue;
1960
1961 if (!(p = strchr(m, '\n')))
1962 p = m + strlen(m);
1963
1964 cli_error(cli, "\r%s-%u-%u %.*s",
1965 debug_levels[(int)ringbuffer->buffer[i].level],
1966 ringbuffer->buffer[i].tunnel,
1967 ringbuffer->buffer[i].session,
1968 (int) (p - m), m);
1969
1970 out++;
1971 }
1972
1973 debug_rb_tail = ringbuffer->tail;
1974 if (out)
1975 cli_reprompt(cli);
1976 #endif
1977 return CLI_OK;
1978 }
1979
1980 #ifdef BGP
1981 static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc)
1982 {
1983 int as;
1984
1985 if (CLI_HELP_REQUESTED)
1986 return cli_arg_help(cli, argc > 1,
1987 "<1-65535>", "Autonomous system number", NULL);
1988
1989 if (argc != 1 || (as = atoi(argv[0])) < 1 || as > 65535)
1990 {
1991 cli_error(cli, "Invalid autonomous system number");
1992 return CLI_OK;
1993 }
1994
1995 if (bgp_configured && as != config->as_number)
1996 {
1997 cli_error(cli, "Can't change local AS on a running system");
1998 return CLI_OK;
1999 }
2000
2001 config->as_number = as;
2002 cli_set_configmode(cli, MODE_CONFIG_BGP, "router");
2003
2004 return CLI_OK;
2005 }
2006
2007 static int find_bgp_neighbour(char const *name)
2008 {
2009 int i;
2010 int new = -1;
2011 struct hostent *h;
2012 in_addr_t addrs[4] = { 0 };
2013 char **a;
2014
2015 if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET)
2016 return -2;
2017
2018 for (i = 0; i < sizeof(addrs) / sizeof(*addrs) && h->h_addr_list[i]; i++)
2019 memcpy(&addrs[i], h->h_addr_list[i], sizeof(*addrs));
2020
2021 for (i = 0; i < BGP_NUM_PEERS; i++)
2022 {
2023 if (!config->neighbour[i].name[0])
2024 {
2025 if (new == -1) new = i;
2026 continue;
2027 }
2028
2029 if (!strcmp(name, config->neighbour[i].name))
2030 return i;
2031
2032 if (!(h = gethostbyname(config->neighbour[i].name)) || h->h_addrtype != AF_INET)
2033 continue;
2034
2035 for (a = h->h_addr_list; *a; a++)
2036 {
2037 int j;
2038 for (j = 0; j < sizeof(addrs) / sizeof(*addrs) && addrs[j]; j++)
2039 if (!memcmp(&addrs[j], *a, sizeof(*addrs)))
2040 return i;
2041 }
2042 }
2043
2044 return new;
2045 }
2046
2047 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc)
2048 {
2049 int i;
2050 int keepalive;
2051 int hold;
2052
2053 if (CLI_HELP_REQUESTED)
2054 {
2055 switch (argc)
2056 {
2057 case 1:
2058 return cli_arg_help(cli, 0,
2059 "A.B.C.D", "BGP neighbour address",
2060 "NAME", "BGP neighbour name",
2061 NULL);
2062
2063 case 2:
2064 return cli_arg_help(cli, 0,
2065 "remote-as", "Set remote autonomous system number",
2066 "timers", "Set timers",
2067 NULL);
2068
2069 default:
2070 if (MATCH("remote-as", argv[1]))
2071 return cli_arg_help(cli, argv[2][1], "<1-65535>", "Autonomous system number", NULL);
2072
2073 if (MATCH("timers", argv[1]))
2074 {
2075 if (argc == 3)
2076 return cli_arg_help(cli, 0, "<1-65535>", "Keepalive time", NULL);
2077
2078 if (argc == 4)
2079 return cli_arg_help(cli, argv[3][1], "<3-65535>", "Hold time", NULL);
2080
2081 if (argc == 5 && !argv[4][1])
2082 return cli_arg_help(cli, 1, NULL);
2083 }
2084
2085 return CLI_OK;
2086 }
2087 }
2088
2089 if (argc < 3)
2090 {
2091 cli_error(cli, "Invalid arguments");
2092 return CLI_OK;
2093 }
2094
2095 if ((i = find_bgp_neighbour(argv[0])) == -2)
2096 {
2097 cli_error(cli, "Invalid neighbour");
2098 return CLI_OK;
2099 }
2100
2101 if (i == -1)
2102 {
2103 cli_error(cli, "Too many neighbours (max %d)", BGP_NUM_PEERS);
2104 return CLI_OK;
2105 }
2106
2107 if (MATCH("remote-as", argv[1]))
2108 {
2109 int as = atoi(argv[2]);
2110 if (as < 0 || as > 65535)
2111 {
2112 cli_error(cli, "Invalid autonomous system number");
2113 return CLI_OK;
2114 }
2115
2116 if (!config->neighbour[i].name[0])
2117 {
2118 snprintf(config->neighbour[i].name, sizeof(config->neighbour[i].name), "%s", argv[0]);
2119 config->neighbour[i].keepalive = -1;
2120 config->neighbour[i].hold = -1;
2121 }
2122
2123 config->neighbour[i].as = as;
2124 return CLI_OK;
2125 }
2126
2127 if (argc != 4 || !MATCH("timers", argv[1]))
2128 {
2129 cli_error(cli, "Invalid arguments");
2130 return CLI_OK;
2131 }
2132
2133 if (!config->neighbour[i].name[0])
2134 {
2135 cli_error(cli, "Specify remote-as first");
2136 return CLI_OK;
2137 }
2138
2139 keepalive = atoi(argv[2]);
2140 hold = atoi(argv[3]);
2141
2142 if (keepalive < 1 || keepalive > 65535)
2143 {
2144 cli_error(cli, "Invalid keepalive time");
2145 return CLI_OK;
2146 }
2147
2148 if (hold < 3 || hold > 65535)
2149 {
2150 cli_error(cli, "Invalid hold time");
2151 return CLI_OK;
2152 }
2153
2154 if (keepalive == BGP_KEEPALIVE_TIME)
2155 keepalive = -1; // using default value
2156
2157 if (hold == BGP_HOLD_TIME)
2158 hold = -1;
2159
2160 config->neighbour[i].keepalive = keepalive;
2161 config->neighbour[i].hold = hold;
2162
2163 return CLI_OK;
2164 }
2165
2166 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc)
2167 {
2168 int i;
2169
2170 if (CLI_HELP_REQUESTED)
2171 return cli_arg_help(cli, argc > 0,
2172 "A.B.C.D", "BGP neighbour address",
2173 "NAME", "BGP neighbour name",
2174 NULL);
2175
2176 if (argc != 1)
2177 {
2178 cli_error(cli, "Specify a BGP neighbour");
2179 return CLI_OK;
2180 }
2181
2182 if ((i = find_bgp_neighbour(argv[0])) == -2)
2183 {
2184 cli_error(cli, "Invalid neighbour");
2185 return CLI_OK;
2186 }
2187
2188 if (i < 0 || !config->neighbour[i].name[0])
2189 {
2190 cli_error(cli, "Neighbour %s not configured", argv[0]);
2191 return CLI_OK;
2192 }
2193
2194 memset(&config->neighbour[i], 0, sizeof(config->neighbour[i]));
2195 return CLI_OK;
2196 }
2197
2198 static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2199 {
2200 int i;
2201 int hdr = 0;
2202 char *addr;
2203
2204 if (!bgp_configured)
2205 return CLI_OK;
2206
2207 if (CLI_HELP_REQUESTED)
2208 return cli_arg_help(cli, 1,
2209 "A.B.C.D", "BGP neighbour address",
2210 "NAME", "BGP neighbour name",
2211 NULL);
2212
2213 cli_print(cli, "BGPv%d router identifier %s, local AS number %d",
2214 BGP_VERSION, fmtaddr(my_address, 0), (int) config->as_number);
2215
2216 time(&time_now);
2217
2218 for (i = 0; i < BGP_NUM_PEERS; i++)
2219 {
2220 if (!*bgp_peers[i].name)
2221 continue;
2222
2223 addr = fmtaddr(bgp_peers[i].addr, 0);
2224 if (argc && strcmp(addr, argv[0]) &&
2225 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2226 continue;
2227
2228 if (!hdr++)
2229 {
2230 cli_print(cli, "");
2231 cli_print(cli, "Peer AS Address "
2232 "State Retries Retry in Route Pend Timers");
2233 cli_print(cli, "------------------ ----- --------------- "
2234 "----------- ------- -------- ----- ---- ---------");
2235 }
2236
2237 cli_print(cli, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2238 bgp_peers[i].name,
2239 bgp_peers[i].as,
2240 addr,
2241 bgp_state_str(bgp_peers[i].state),
2242 bgp_peers[i].retry_count,
2243 bgp_peers[i].retry_time ? bgp_peers[i].retry_time - time_now : 0,
2244 bgp_peers[i].routing ? "yes" : "no",
2245 bgp_peers[i].update_routes ? "yes" : "no",
2246 bgp_peers[i].keepalive,
2247 bgp_peers[i].hold);
2248 }
2249
2250 return CLI_OK;
2251 }
2252
2253 static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2254 {
2255 int i;
2256 char *addr;
2257
2258 if (!bgp_configured)
2259 return CLI_OK;
2260
2261 if (CLI_HELP_REQUESTED)
2262 return cli_arg_help(cli, 1,
2263 "A.B.C.D", "BGP neighbour address",
2264 "NAME", "BGP neighbour name",
2265 NULL);
2266
2267 for (i = 0; i < BGP_NUM_PEERS; i++)
2268 {
2269 if (bgp_peers[i].state != Established)
2270 continue;
2271
2272 if (!bgp_peers[i].routing)
2273 continue;
2274
2275 addr = fmtaddr(bgp_peers[i].addr, 0);
2276 if (argc && strcmp(addr, argv[0]) && strcmp(bgp_peers[i].name, argv[0]))
2277 continue;
2278
2279 bgp_peers[i].cli_flag = BGP_CLI_SUSPEND;
2280 cli_print(cli, "Suspending peer %s", bgp_peers[i].name);
2281 }
2282
2283 return CLI_OK;
2284 }
2285
2286 static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2287 {
2288 int i;
2289 char *addr;
2290
2291 if (!bgp_configured)
2292 return CLI_OK;
2293
2294 if (CLI_HELP_REQUESTED)
2295 return cli_arg_help(cli, 1,
2296 "A.B.C.D", "BGP neighbour address",
2297 "NAME", "BGP neighbour name",
2298 NULL);
2299
2300 for (i = 0; i < BGP_NUM_PEERS; i++)
2301 {
2302 if (bgp_peers[i].state != Established)
2303 continue;
2304
2305 if (bgp_peers[i].routing)
2306 continue;
2307
2308 addr = fmtaddr(bgp_peers[i].addr, 0);
2309 if (argc && strcmp(addr, argv[0]) &&
2310 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2311 continue;
2312
2313 bgp_peers[i].cli_flag = BGP_CLI_ENABLE;
2314 cli_print(cli, "Un-suspending peer %s", bgp_peers[i].name);
2315 }
2316
2317 return CLI_OK;
2318 }
2319
2320 static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc)
2321 {
2322 int i;
2323 char *addr;
2324
2325 if (!bgp_configured)
2326 return CLI_OK;
2327
2328 if (CLI_HELP_REQUESTED)
2329 return cli_arg_help(cli, 1,
2330 "A.B.C.D", "BGP neighbour address",
2331 "NAME", "BGP neighbour name",
2332 NULL);
2333
2334 for (i = 0; i < BGP_NUM_PEERS; i++)
2335 {
2336 if (!*bgp_peers[i].name)
2337 continue;
2338
2339 addr = fmtaddr(bgp_peers[i].addr, 0);
2340 if (argc && strcmp(addr, argv[0]) &&
2341 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2342 continue;
2343
2344 bgp_peers[i].cli_flag = BGP_CLI_RESTART;
2345 cli_print(cli, "Restarting peer %s", bgp_peers[i].name);
2346 }
2347
2348 return CLI_OK;
2349 }
2350 #endif /* BGP*/
2351
2352 static int filt;
2353 static int access_list(struct cli_def *cli, char **argv, int argc, int add)
2354 {
2355 int extended;
2356
2357 if (CLI_HELP_REQUESTED)
2358 {
2359 switch (argc)
2360 {
2361 case 1:
2362 return cli_arg_help(cli, 0,
2363 "standard", "Standard syntax",
2364 "extended", "Extended syntax",
2365 NULL);
2366
2367 case 2:
2368 return cli_arg_help(cli, argv[1][1],
2369 "NAME", "Access-list name",
2370 NULL);
2371
2372 default:
2373 if (argc == 3 && !argv[2][1])
2374 return cli_arg_help(cli, 1, NULL);
2375
2376 return CLI_OK;
2377 }
2378 }
2379
2380 if (argc != 2)
2381 {
2382 cli_error(cli, "Specify access-list type and name");
2383 return CLI_OK;
2384 }
2385
2386 if (MATCH("standard", argv[0]))
2387 extended = 0;
2388 else if (MATCH("extended", argv[0]))
2389 extended = 1;
2390 else
2391 {
2392 cli_error(cli, "Invalid access-list type");
2393 return CLI_OK;
2394 }
2395
2396 if (strlen(argv[1]) > sizeof(ip_filters[0].name) - 1 ||
2397 strspn(argv[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv[1]))
2398 {
2399 cli_error(cli, "Invalid access-list name");
2400 return CLI_OK;
2401 }
2402
2403 filt = find_filter(argv[1], strlen(argv[1]));
2404 if (add)
2405 {
2406 if (filt < 0)
2407 {
2408 cli_error(cli, "Too many access-lists");
2409 return CLI_OK;
2410 }
2411
2412 // racy
2413 if (!*ip_filters[filt].name)
2414 {
2415 memset(&ip_filters[filt], 0, sizeof(ip_filters[filt]));
2416 strcpy(ip_filters[filt].name, argv[1]);
2417 ip_filters[filt].extended = extended;
2418 }
2419 else if (ip_filters[filt].extended != extended)
2420 {
2421 cli_error(cli, "Access-list is %s",
2422 ip_filters[filt].extended ? "extended" : "standard");
2423
2424 return CLI_OK;
2425 }
2426
2427 cli_set_configmode(cli, MODE_CONFIG_NACL, extended ? "ext-nacl" : "std-nacl");
2428 return CLI_OK;
2429 }
2430
2431 if (filt < 0 || !*ip_filters[filt].name)
2432 {
2433 cli_error(cli, "Access-list not defined");
2434 return CLI_OK;
2435 }
2436
2437 // racy
2438 if (ip_filters[filt].used)
2439 {
2440 cli_error(cli, "Access-list in use");
2441 return CLI_OK;
2442 }
2443
2444 memset(&ip_filters[filt], 0, sizeof(ip_filters[filt]));
2445 return CLI_OK;
2446 }
2447
2448 static int cmd_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc)
2449 {
2450 return access_list(cli, argv, argc, 1);
2451 }
2452
2453 static int cmd_no_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc)
2454 {
2455 return access_list(cli, argv, argc, 0);
2456 }
2457
2458 static int show_ip_wild(char *buf, in_addr_t ip, in_addr_t wild)
2459 {
2460 if (ip == INADDR_ANY && wild == INADDR_BROADCAST)
2461 return sprintf(buf, " any");
2462
2463 if (wild == INADDR_ANY)
2464 return sprintf(buf, " host %s", fmtaddr(ip, 0));
2465
2466 return sprintf(buf, " %s %s", fmtaddr(ip, 0), fmtaddr(wild, 1));
2467 }
2468
2469 static int show_ports(char *buf, ip_filter_portt *ports)
2470 {
2471 switch (ports->op)
2472 {
2473 case FILTER_PORT_OP_EQ: return sprintf(buf, " eq %u", ports->port);
2474 case FILTER_PORT_OP_NEQ: return sprintf(buf, " neq %u", ports->port);
2475 case FILTER_PORT_OP_GT: return sprintf(buf, " gt %u", ports->port);
2476 case FILTER_PORT_OP_LT: return sprintf(buf, " lt %u", ports->port);
2477 case FILTER_PORT_OP_RANGE: return sprintf(buf, " range %u %u", ports->port, ports->port2);
2478 }
2479
2480 return 0;
2481 }
2482
2483 static char const *show_access_list_rule(int extended, ip_filter_rulet *rule)
2484 {
2485 static char buf[256];
2486 char *p = buf;
2487
2488 p += sprintf(p, " %s", rule->action == FILTER_ACTION_PERMIT ? "permit" : "deny");
2489 if (extended)
2490 {
2491 struct protoent *proto = getprotobynumber(rule->proto);
2492 p += sprintf(p, " %s", proto ? proto->p_name : "ERR");
2493 }
2494
2495 p += show_ip_wild(p, rule->src_ip, rule->src_wild);
2496 if (!extended)
2497 return buf;
2498
2499 if (rule->proto == IPPROTO_TCP || rule->proto == IPPROTO_UDP)
2500 p += show_ports(p, &rule->src_ports);
2501
2502 p += show_ip_wild(p, rule->dst_ip, rule->dst_wild);
2503 if (rule->proto == IPPROTO_TCP || rule->proto == IPPROTO_UDP)
2504 p += show_ports(p, &rule->dst_ports);
2505
2506 if (rule->proto == IPPROTO_TCP && rule->tcp_flag_op)
2507 {
2508 switch (rule->tcp_flag_op)
2509 {
2510 case FILTER_FLAG_OP_EST:
2511 p += sprintf(p, " established");
2512 break;
2513
2514 case FILTER_FLAG_OP_ANY:
2515 case FILTER_FLAG_OP_ALL:
2516 p += sprintf(p, " match-%s", rule->tcp_flag_op == FILTER_FLAG_OP_ALL ? "all" : "any");
2517 if (rule->tcp_sflags & TCP_FLAG_FIN) p += sprintf(p, " +fin");
2518 if (rule->tcp_cflags & TCP_FLAG_FIN) p += sprintf(p, " -fin");
2519 if (rule->tcp_sflags & TCP_FLAG_SYN) p += sprintf(p, " +syn");
2520 if (rule->tcp_cflags & TCP_FLAG_SYN) p += sprintf(p, " -syn");
2521 if (rule->tcp_sflags & TCP_FLAG_RST) p += sprintf(p, " +rst");
2522 if (rule->tcp_cflags & TCP_FLAG_RST) p += sprintf(p, " -rst");
2523 if (rule->tcp_sflags & TCP_FLAG_PSH) p += sprintf(p, " +psh");
2524 if (rule->tcp_cflags & TCP_FLAG_PSH) p += sprintf(p, " -psh");
2525 if (rule->tcp_sflags & TCP_FLAG_ACK) p += sprintf(p, " +ack");
2526 if (rule->tcp_cflags & TCP_FLAG_ACK) p += sprintf(p, " -ack");
2527 if (rule->tcp_sflags & TCP_FLAG_URG) p += sprintf(p, " +urg");
2528 if (rule->tcp_cflags & TCP_FLAG_URG) p += sprintf(p, " -urg");
2529 break;
2530 }
2531 }
2532
2533 if (rule->frag)
2534 p += sprintf(p, " fragments");
2535
2536 return buf;
2537 }
2538
2539 static ip_filter_rulet *access_list_rule_ext(struct cli_def *cli, char *command, char **argv, int argc)
2540 {
2541 static ip_filter_rulet rule;
2542 struct in_addr addr;
2543 int i;
2544 int a;
2545
2546 if (CLI_HELP_REQUESTED)
2547 {
2548 if (argc == 1)
2549 {
2550 cli_arg_help(cli, 0,
2551 "ip", "Match IP packets",
2552 "tcp", "Match TCP packets",
2553 "udp", "Match UDP packets",
2554 NULL);
2555
2556 return NULL;
2557 }
2558
2559 // *sigh*, too darned complex
2560 cli_arg_help(cli, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL);
2561 return NULL;
2562 }
2563
2564 if (argc < 3)
2565 {
2566 cli_error(cli, "Specify rule details");
2567 return NULL;
2568 }
2569
2570 memset(&rule, 0, sizeof(rule));
2571 rule.action = (command[0] == 'p')
2572 ? FILTER_ACTION_PERMIT
2573 : FILTER_ACTION_DENY;
2574
2575 if (MATCH("ip", argv[0]))
2576 rule.proto = IPPROTO_IP;
2577 else if (MATCH("udp", argv[0]))
2578 rule.proto = IPPROTO_UDP;
2579 else if (MATCH("tcp", argv[0]))
2580 rule.proto = IPPROTO_TCP;
2581 else
2582 {
2583 cli_error(cli, "Invalid protocol \"%s\"", argv[0]);
2584 return NULL;
2585 }
2586
2587 for (a = 1, i = 0; i < 2; i++)
2588 {
2589 in_addr_t *ip;
2590 in_addr_t *wild;
2591 ip_filter_portt *port;
2592
2593 if (i == 0)
2594 {
2595 ip = &rule.src_ip;
2596 wild = &rule.src_wild;
2597 port = &rule.src_ports;
2598 }
2599 else
2600 {
2601 ip = &rule.dst_ip;
2602 wild = &rule.dst_wild;
2603 port = &rule.dst_ports;
2604 if (a >= argc)
2605 {
2606 cli_error(cli, "Specify destination");
2607 return NULL;
2608 }
2609 }
2610
2611 if (MATCH("any", argv[a]))
2612 {
2613 *ip = INADDR_ANY;
2614 *wild = INADDR_BROADCAST;
2615 a++;
2616 }
2617 else if (MATCH("host", argv[a]))
2618 {
2619 if (++a >= argc)
2620 {
2621 cli_error(cli, "Specify host ip address");
2622 return NULL;
2623 }
2624
2625 if (!inet_aton(argv[a], &addr))
2626 {
2627 cli_error(cli, "Cannot parse IP \"%s\"", argv[a]);
2628 return NULL;
2629 }
2630
2631 *ip = addr.s_addr;
2632 *wild = INADDR_ANY;
2633 a++;
2634 }
2635 else
2636 {
2637 if (a >= argc - 1)
2638 {
2639 cli_error(cli, "Specify %s ip address and wildcard", i ? "destination" : "source");
2640 return NULL;
2641 }
2642
2643 if (!inet_aton(argv[a], &addr))
2644 {
2645 cli_error(cli, "Cannot parse IP \"%s\"", argv[a]);
2646 return NULL;
2647 }
2648
2649 *ip = addr.s_addr;
2650
2651 if (!inet_aton(argv[++a], &addr))
2652 {
2653 cli_error(cli, "Cannot parse IP \"%s\"", argv[a]);
2654 return NULL;
2655 }
2656
2657 *wild = addr.s_addr;
2658 a++;
2659 }
2660
2661 if (rule.proto == IPPROTO_IP || a >= argc)
2662 continue;
2663
2664 port->op = 0;
2665 if (MATCH("eq", argv[a]))
2666 port->op = FILTER_PORT_OP_EQ;
2667 else if (MATCH("neq", argv[a]))
2668 port->op = FILTER_PORT_OP_NEQ;
2669 else if (MATCH("gt", argv[a]))
2670 port->op = FILTER_PORT_OP_GT;
2671 else if (MATCH("lt", argv[a]))
2672 port->op = FILTER_PORT_OP_LT;
2673 else if (MATCH("range", argv[a]))
2674 port->op = FILTER_PORT_OP_RANGE;
2675
2676 if (!port->op)
2677 continue;
2678
2679 if (++a >= argc)
2680 {
2681 cli_error(cli, "Specify port");
2682 return NULL;
2683 }
2684
2685 if (!(port->port = atoi(argv[a])))
2686 {
2687 cli_error(cli, "Invalid port \"%s\"", argv[a]);
2688 return NULL;
2689 }
2690
2691 a++;
2692 if (port->op != FILTER_PORT_OP_RANGE)
2693 continue;
2694
2695 if (a >= argc)
2696 {
2697 cli_error(cli, "Specify port");
2698 return NULL;
2699 }
2700
2701 if (!(port->port2 = atoi(argv[a])) || port->port2 < port->port)
2702 {
2703 cli_error(cli, "Invalid port \"%s\"", argv[a]);
2704 return NULL;
2705 }
2706
2707 a++;
2708 }
2709
2710 if (rule.proto == IPPROTO_TCP && a < argc)
2711 {
2712 if (MATCH("established", argv[a]))
2713 {
2714 rule.tcp_flag_op = FILTER_FLAG_OP_EST;
2715 a++;
2716 }
2717 else if (!strcmp(argv[a], "match-any") || !strcmp(argv[a], "match-an") ||
2718 !strcmp(argv[a], "match-all") || !strcmp(argv[a], "match-al"))
2719 {
2720 rule.tcp_flag_op = argv[a][7] == 'n'
2721 ? FILTER_FLAG_OP_ANY
2722 : FILTER_FLAG_OP_ALL;
2723
2724 if (++a >= argc)
2725 {
2726 cli_error(cli, "Specify tcp flags");
2727 return NULL;
2728 }
2729
2730 while (a < argc && (argv[a][0] == '+' || argv[a][0] == '-'))
2731 {
2732 uint8_t *f;
2733
2734 f = (argv[a][0] == '+') ? &rule.tcp_sflags : &rule.tcp_cflags;
2735
2736 if (MATCH("fin", &argv[a][1])) *f |= TCP_FLAG_FIN;
2737 else if (MATCH("syn", &argv[a][1])) *f |= TCP_FLAG_SYN;
2738 else if (MATCH("rst", &argv[a][1])) *f |= TCP_FLAG_RST;
2739 else if (MATCH("psh", &argv[a][1])) *f |= TCP_FLAG_PSH;
2740 else if (MATCH("ack", &argv[a][1])) *f |= TCP_FLAG_ACK;
2741 else if (MATCH("urg", &argv[a][1])) *f |= TCP_FLAG_URG;
2742 else
2743 {
2744 cli_error(cli, "Invalid tcp flag \"%s\"", argv[a]);
2745 return NULL;
2746 }
2747
2748 a++;
2749 }
2750 }
2751 }
2752
2753 if (a < argc && MATCH("fragments", argv[a]))
2754 {
2755 if (rule.src_ports.op || rule.dst_ports.op || rule.tcp_flag_op)
2756 {
2757 cli_error(cli, "Can't specify \"fragments\" on rules with layer 4 matches");
2758 return NULL;
2759 }
2760
2761 rule.frag = 1;
2762 a++;
2763 }
2764
2765 if (a < argc)
2766 {
2767 cli_error(cli, "Invalid flag \"%s\"", argv[a]);
2768 return NULL;
2769 }
2770
2771 return &rule;
2772 }
2773
2774 static ip_filter_rulet *access_list_rule_std(struct cli_def *cli, char *command, char **argv, int argc)
2775 {
2776 static ip_filter_rulet rule;
2777 struct in_addr addr;
2778
2779 if (CLI_HELP_REQUESTED)
2780 {
2781 if (argc == 1)
2782 {
2783 cli_arg_help(cli, argv[0][1],
2784 "A.B.C.D", "Source address",
2785 "any", "Any source address",
2786 "host", "Source host",
2787 NULL);
2788
2789 return NULL;
2790 }
2791
2792 if (MATCH("any", argv[0]))
2793 {
2794 if (argc == 2 && !argv[1][1])
2795 cli_arg_help(cli, 1, NULL);
2796 }
2797 else if (MATCH("host", argv[0]))
2798 {
2799 if (argc == 2)
2800 {
2801 cli_arg_help(cli, argv[1][1],
2802 "A.B.C.D", "Host address",
2803 NULL);
2804 }
2805 else if (argc == 3 && !argv[2][1])
2806 cli_arg_help(cli, 1, NULL);
2807 }
2808 else
2809 {
2810 if (argc == 2)
2811 {
2812 cli_arg_help(cli, 1,
2813 "A.B.C.D", "Wildcard bits",
2814 NULL);
2815 }
2816 else if (argc == 3 && !argv[2][1])
2817 cli_arg_help(cli, 1, NULL);
2818 }
2819
2820 return NULL;
2821 }
2822
2823 if (argc < 1)
2824 {
2825 cli_error(cli, "Specify rule details");
2826 return NULL;
2827 }
2828
2829 memset(&rule, 0, sizeof(rule));
2830 rule.action = (command[0] == 'p')
2831 ? FILTER_ACTION_PERMIT
2832 : FILTER_ACTION_DENY;
2833
2834 rule.proto = IPPROTO_IP;
2835 if (MATCH("any", argv[0]))
2836 {
2837 rule.src_ip = INADDR_ANY;
2838 rule.src_wild = INADDR_BROADCAST;
2839 }
2840 else if (MATCH("host", argv[0]))
2841 {
2842 if (argc != 2)
2843 {
2844 cli_error(cli, "Specify host ip address");
2845 return NULL;
2846 }
2847
2848 if (!inet_aton(argv[1], &addr))
2849 {
2850 cli_error(cli, "Cannot parse IP \"%s\"", argv[1]);
2851 return NULL;
2852 }
2853
2854 rule.src_ip = addr.s_addr;
2855 rule.src_wild = INADDR_ANY;
2856 }
2857 else
2858 {
2859 if (argc > 2)
2860 {
2861 cli_error(cli, "Specify source ip address and wildcard");
2862 return NULL;
2863 }
2864
2865 if (!inet_aton(argv[0], &addr))
2866 {
2867 cli_error(cli, "Cannot parse IP \"%s\"", argv[0]);
2868 return NULL;
2869 }
2870
2871 rule.src_ip = addr.s_addr;
2872
2873 if (argc > 1)
2874 {
2875 if (!inet_aton(argv[1], &addr))
2876 {
2877 cli_error(cli, "Cannot parse IP \"%s\"", argv[1]);
2878 return NULL;
2879 }
2880
2881 rule.src_wild = addr.s_addr;
2882 }
2883 else
2884 rule.src_wild = INADDR_ANY;
2885 }
2886
2887 return &rule;
2888 }
2889
2890 static int cmd_ip_access_list_rule(struct cli_def *cli, char *command, char **argv, int argc)
2891 {
2892 int i;
2893 ip_filter_rulet *rule = ip_filters[filt].extended
2894 ? access_list_rule_ext(cli, command, argv, argc)
2895 : access_list_rule_std(cli, command, argv, argc);
2896
2897 if (!rule)
2898 return CLI_OK;
2899
2900 for (i = 0; i < MAXFILTER_RULES - 1; i++) // -1: list always terminated by empty rule
2901 {
2902 if (!ip_filters[filt].rules[i].action)
2903 {
2904 memcpy(&ip_filters[filt].rules[i], rule, sizeof(*rule));
2905 return CLI_OK;
2906 }
2907
2908 if (!memcmp(&ip_filters[filt].rules[i], rule, offsetof(ip_filter_rulet, counter)))
2909 return CLI_OK;
2910 }
2911
2912 cli_error(cli, "Too many rules");
2913 return CLI_OK;
2914 }
2915
2916 static int cmd_filter(struct cli_def *cli, char *command, char **argv, int argc)
2917 {
2918 sessionidt s;
2919 int i;
2920
2921 /* filter USER {in|out} FILTER ... */
2922 if (CLI_HELP_REQUESTED)
2923 {
2924 switch (argc)
2925 {
2926 case 1:
2927 return cli_arg_help(cli, 0,
2928 "USER", "Username of session to filter", NULL);
2929
2930 case 2:
2931 case 4:
2932 return cli_arg_help(cli, 0,
2933 "in", "Set incoming filter",
2934 "out", "Set outgoing filter", NULL);
2935
2936 case 3:
2937 case 5:
2938 return cli_arg_help(cli, argc == 5 && argv[4][1],
2939 "NAME", "Filter name", NULL);
2940
2941 default:
2942 return cli_arg_help(cli, argc > 1, NULL);
2943 }
2944 }
2945
2946 if (!config->cluster_iam_master)
2947 {
2948 cli_error(cli, "Can't do this on a slave. Do it on %s",
2949 fmtaddr(config->cluster_master_address, 0));
2950
2951 return CLI_OK;
2952 }
2953
2954 if (argc != 3 && argc != 5)
2955 {
2956 cli_error(cli, "Specify a user and filters");
2957 return CLI_OK;
2958 }
2959
2960 if (!(s = sessionbyuser(argv[0])))
2961 {
2962 cli_error(cli, "User %s is not connected", argv[0]);
2963 return CLI_OK;
2964 }
2965
2966 cli_session_actions[s].filter_in = cli_session_actions[s].filter_out = -1;
2967 for (i = 1; i < argc; i += 2)
2968 {
2969 int *f = 0;
2970 int v;
2971
2972 if (MATCH("in", argv[i]))
2973 {
2974 if (session[s].filter_in)
2975 {
2976 cli_error(cli, "Input already filtered");
2977 return CLI_OK;
2978 }
2979 f = &cli_session_actions[s].filter_in;
2980 }
2981 else if (MATCH("out", argv[i]))
2982 {
2983 if (session[s].filter_out)
2984 {
2985 cli_error(cli, "Output already filtered");
2986 return CLI_OK;
2987 }
2988 f = &cli_session_actions[s].filter_out;
2989 }
2990 else
2991 {
2992 cli_error(cli, "Invalid filter specification");
2993 return CLI_OK;
2994 }
2995
2996 v = find_filter(argv[i+1], strlen(argv[i+1]));
2997 if (v < 0 || !*ip_filters[v].name)
2998 {
2999 cli_error(cli, "Access-list %s not defined", argv[i+1]);
3000 return CLI_OK;
3001 }
3002
3003 *f = v + 1;
3004 }
3005
3006 cli_print(cli, "Filtering user %s", argv[0]);
3007 cli_session_actions[s].action |= CLI_SESS_FILTER;
3008
3009 return CLI_OK;
3010 }
3011
3012 static int cmd_no_filter(struct cli_def *cli, char *command, char **argv, int argc)
3013 {
3014 int i;
3015 sessionidt s;
3016
3017 if (CLI_HELP_REQUESTED)
3018 return cli_arg_help(cli, argc > 1,
3019 "USER", "Username of session to remove filters from", NULL);
3020
3021 if (!config->cluster_iam_master)
3022 {
3023 cli_error(cli, "Can't do this on a slave. Do it on %s",
3024 fmtaddr(config->cluster_master_address, 0));
3025
3026 return CLI_OK;
3027 }
3028
3029 if (!argc)
3030 {
3031 cli_error(cli, "Specify a user to remove filters from");
3032 return CLI_OK;
3033 }
3034
3035 for (i = 0; i < argc; i++)
3036 {
3037 if (!(s = sessionbyuser(argv[i])))
3038 {
3039 cli_error(cli, "User %s is not connected", argv[i]);
3040 continue;
3041 }
3042
3043 if (session[s].filter_in || session[s].filter_out)
3044 {
3045 cli_print(cli, "Removing filters from user %s", argv[i]);
3046 cli_session_actions[s].action |= CLI_SESS_NOFILTER;
3047 }
3048 else
3049 {
3050 cli_error(cli, "User %s not filtered", argv[i]);
3051 }
3052 }
3053
3054 return CLI_OK;
3055 }
3056
3057 static int cmd_show_access_list(struct cli_def *cli, char *command, char **argv, int argc)
3058 {
3059 int i;
3060
3061 if (CLI_HELP_REQUESTED)
3062 return cli_arg_help(cli, argc > 1, "NAME", "Filter name", NULL);
3063
3064 if (argc < 1)
3065 {
3066 cli_error(cli, "Specify a filter name");
3067 return CLI_OK;
3068 }
3069
3070 for (i = 0; i < argc; i++)
3071 {
3072 int f = find_filter(argv[i], strlen(argv[i]));
3073 ip_filter_rulet *rules;
3074
3075 if (f < 0 || !*ip_filters[f].name)
3076 {
3077 cli_error(cli, "Access-list %s not defined", argv[i]);
3078 return CLI_OK;
3079 }
3080
3081 if (i)
3082 cli_print(cli, "");
3083
3084 cli_print(cli, "%s IP access list %s",
3085 ip_filters[f].extended ? "Extended" : "Standard",
3086 ip_filters[f].name);
3087
3088 for (rules = ip_filters[f].rules; rules->action; rules++)
3089 {
3090 char const *r = show_access_list_rule(ip_filters[f].extended, rules);
3091 if (rules->counter)
3092 cli_print(cli, "%s (%u match%s)", r,
3093 rules->counter, rules->counter > 1 ? "es" : "");
3094 else
3095 cli_print(cli, "%s", r);
3096 }
3097 }
3098
3099 return CLI_OK;
3100 }
3101
3102 static int cmd_shutdown(struct cli_def *cli, char *command, char **argv, int argc)
3103 {
3104 if (CLI_HELP_REQUESTED)
3105 return CLI_HELP_NO_ARGS;
3106
3107 kill(getppid(), SIGQUIT);
3108 return CLI_OK;
3109 }
3110
3111 static int cmd_reload(struct cli_def *cli, char *command, char **argv, int argc)
3112 {
3113 if (CLI_HELP_REQUESTED)
3114 return CLI_HELP_NO_ARGS;
3115
3116 kill(getppid(), SIGHUP);
3117 return CLI_OK;
3118 }