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