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