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