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