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