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