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