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