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