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