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