fix inverted timeout test
[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.65 2005/08/10 07:53:56 bodea Exp $";
6
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <unistd.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <syslog.h>
13 #include <malloc.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <signal.h>
23 #include <dlfcn.h>
24 #include <netdb.h>
25 #include <libcli.h>
26
27 #include "l2tpns.h"
28 #include "constants.h"
29 #include "util.h"
30 #include "cluster.h"
31 #include "tbf.h"
32 #include "ll.h"
33 #ifdef BGP
34 #include "bgp.h"
35 #endif
36
37 extern tunnelt *tunnel;
38 extern sessiont *session;
39 extern radiust *radius;
40 extern ippoolt *ip_address_pool;
41 extern struct Tstats *_statistics;
42 static struct cli_def *cli = NULL;
43 extern configt *config;
44 extern config_descriptt config_values[];
45 #ifdef RINGBUFFER
46 extern struct Tringbuffer *ringbuffer;
47 #endif
48 extern struct cli_session_actions *cli_session_actions;
49 extern struct cli_tunnel_actions *cli_tunnel_actions;
50 extern tbft *filter_list;
51 extern ip_filtert *ip_filters;
52
53 struct
54 {
55 char critical;
56 char error;
57 char warning;
58 char info;
59 char calls;
60 char data;
61 } debug_flags;
62
63 #ifdef RINGBUFFER
64
65 static int debug_rb_tail;
66 static char *debug_levels[] = {
67 "CRIT",
68 "ERROR",
69 "WARN",
70 "INFO",
71 "CALL",
72 "DATA",
73 };
74
75 #endif
76
77 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
78 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
79 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
80 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
81 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc);
82 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc);
83 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc);
84 static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc);
85 static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc);
86 static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc);
87 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc);
88 static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc);
89 static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc);
90 static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc);
91 static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc);
92 static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc);
93 static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc);
94 static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc);
95 static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc);
96 static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc);
97 static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
98 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
99 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
100 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
101
102 static int regular_stuff(struct cli_def *cli);
103 static void parsemac(char *string, char mac[6]);
104
105 #ifdef STATISTICS
106 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc);
107 static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc);
108 #endif /* STATISTICS */
109
110 #ifdef BGP
111 #define MODE_CONFIG_BGP 8
112 static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc);
113 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
114 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, char *command, char **argv, int argc);
115 static int cmd_show_bgp(struct cli_def *cli, char *command, char **argv, int argc);
116 static int cmd_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
117 static int cmd_no_suspend_bgp(struct cli_def *cli, char *command, char **argv, int argc);
118 static int cmd_restart_bgp(struct cli_def *cli, char *command, char **argv, int argc);
119 #endif /* BGP */
120
121 #define MODE_CONFIG_NACL 9
122 static int cmd_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc);
123 static int cmd_no_ip_access_list(struct cli_def *cli, char *command, char **argv, int argc);
124 static int cmd_ip_access_list_rule(struct cli_def *cli, char *command, char **argv, int argc);
125 static int cmd_filter(struct cli_def *cli, char *command, char **argv, int argc);
126 static int cmd_no_filter(struct cli_def *cli, char *command, char **argv, int argc);
127 static int cmd_show_access_list(struct cli_def *cli, char *command, char **argv, int argc);
128
129 /* match if b is a substr of a */
130 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
131
132 void init_cli(char *hostname)
133 {
134 FILE *f;
135 char buf[4096];
136 struct cli_command *c;
137 struct cli_command *c2;
138 int on = 1;
139 struct sockaddr_in addr;
140
141 cli = cli_init();
142 if (hostname && *hostname)
143 cli_set_hostname(cli, hostname);
144 else
145 cli_set_hostname(cli, "l2tpns");
146
147 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
148 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
149 #ifdef BGP
150 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
151 #endif /* BGP */
152 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
153 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
154 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
155 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
156 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
157 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Show the currently running configuration");
158 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
159 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
160 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
161 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
162 cli_register_command(cli, c, "users", cmd_show_users, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of all connected users or details of selected user");
163 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
164 cli_register_command(cli, c, "access-list", cmd_show_access_list, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show named access-list");
165
166 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
167 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
168 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
169
170 #ifdef STATISTICS
171 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
172
173 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
174 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
175 #endif
176
177 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
178
179 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
180 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
181 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
182
183 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable interception of a session");
184 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable throttling of a session");
185 cli_register_command(cli, NULL, "filter", cmd_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Add filtering to a session");
186 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
187
188 #ifdef BGP
189 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
190 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP neighbour");
191 #endif /* BGP */
192
193 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
194 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable interception of a session");
195 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable throttling of a session");
196 cli_register_command(cli, c, "filter", cmd_no_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Remove filtering from a session");
197 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
198
199 #ifdef BGP
200 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
201 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP neighbour");
202
203 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
204 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
205
206 c = cli_register_command(cli, NULL, "router", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
207 cli_register_command(cli, c, "bgp", cmd_router_bgp, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Configure BGP");
208
209 cli_register_command(cli, NULL, "neighbour", cmd_router_bgp_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Configure BGP neighbour");
210
211 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, NULL);
212 cli_register_command(cli, c, "neighbour", cmd_router_bgp_no_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Remove BGP neighbour");
213 #endif /* BGP */
214
215 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
216 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
217 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
218 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
219
220 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
221 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
222
223 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
224 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
225
226 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
227
228 c = cli_register_command(cli, NULL, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
229 cli_register_command(cli, c, "access-list", cmd_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Add named access-list");
230
231 cli_register_command(cli, NULL, "permit", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Permit rule");
232 cli_register_command(cli, NULL, "deny", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Deny rule");
233
234 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_CONFIG, NULL);
235 c2 = cli_register_command(cli, c, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
236 cli_register_command(cli, c2, "access-list", cmd_no_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove named access-list");
237
238 // Enable regular processing
239 cli_regular(cli, regular_stuff);
240
241 if (!(f = fopen(CLIUSERS, "r")))
242 {
243 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
244 }
245 else
246 {
247 while (fgets(buf, 4096, f))
248 {
249 char *p;
250 if (*buf == '#') continue;
251 if ((p = strchr(buf, '\r'))) *p = 0;
252 if ((p = strchr(buf, '\n'))) *p = 0;
253 if (!*buf) continue;
254 if (!(p = strchr((char *)buf, ':'))) continue;
255 *p++ = 0;
256 if (!strcmp(buf, "enable"))
257 {
258 cli_allow_enable(cli, p);
259 LOG(3, 0, 0, "Setting enable password\n");
260 }
261 else
262 {
263 cli_allow_user(cli, buf, p);
264 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
265 }
266 }
267 fclose(f);
268 }
269
270 memset(&addr, 0, sizeof(addr));
271 clifd = socket(PF_INET, SOCK_STREAM, 6);
272 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
273 {
274 int flags;
275 // Set cli fd as non-blocking
276 flags = fcntl(clifd, F_GETFL, 0);
277 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
278 }
279 addr.sin_family = AF_INET;
280 addr.sin_port = htons(23);
281 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
282 {
283 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
284 return;
285 }
286 listen(clifd, 10);
287 }
288
289 void cli_do(int sockfd)
290 {
291 int require_auth = 1;
292 struct sockaddr_in addr;
293 socklen_t l = sizeof(addr);
294
295 if (fork_and_close()) return;
296 if (getpeername(sockfd, (struct sockaddr *) &addr, &l) == 0)
297 {
298 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
299 LOG(require_auth ? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
300 fmtaddr(addr.sin_addr.s_addr, 0));
301 }
302 else
303 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
304
305 if (require_auth)
306 {
307 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
308 if (!cli->users) /* paranoia */
309 {
310 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
311 exit(0);
312 }
313 }
314 else
315 {
316 /* no username/pass required */
317 cli->users = 0;
318 }
319
320 #ifdef RINGBUFFER
321 debug_rb_tail = ringbuffer->tail;
322 #endif
323 memset(&debug_flags, 0, sizeof(debug_flags));
324 debug_flags.critical = 1;
325
326 cli_loop(cli, sockfd);
327
328 close(sockfd);
329 LOG(require_auth ? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
330 fmtaddr(addr.sin_addr.s_addr, 0));
331
332 exit(0);
333 }
334
335 static void cli_print_log(struct cli_def *cli, char *string)
336 {
337 LOG(3, 0, 0, "%s\n", string);
338 }
339
340 void cli_do_file(FILE *fh)
341 {
342 LOG(3, 0, 0, "Reading configuration file\n");
343 cli_print_callback(cli, cli_print_log);
344 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
345 cli_print_callback(cli, NULL);
346 }
347
348 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
349 {
350 va_list ap;
351 char *desc;
352 char buf[16];
353 char *p;
354
355 va_start(ap, entry);
356 while (entry)
357 {
358 /* allow one %d */
359 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
360 {
361 int v = va_arg(ap, int);
362 snprintf(buf, sizeof(buf), entry, v);
363 p = buf;
364 }
365 else
366 p = entry;
367
368 desc = va_arg(ap, char *);
369 if (desc && *desc)
370 cli_error(cli, " %-20s %s", p, desc);
371 else
372 cli_error(cli, " %s", p);
373
374 entry = desc ? va_arg(ap, char *) : 0;
375 }
376
377 va_end(ap);
378 if (cr_ok)
379 cli_error(cli, " <cr>");
380
381 return CLI_OK;
382 }
383
384 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc)
385 {
386 int i;
387
388 if (CLI_HELP_REQUESTED)
389 return cli_arg_help(cli, 1,
390 "<1-%d>", MAXSESSION-1, "Show specific session by id",
391 NULL);
392
393 time(&time_now);
394 if (argc > 0)
395 {
396 // Show individual session
397 for (i = 0; i < argc; i++)
398 {
399 unsigned int s, b_in, b_out;
400 s = atoi(argv[i]);
401 if (s <= 0 || s >= MAXSESSION)
402 {
403 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
404 continue;
405 }
406 cli_print(cli, "\r\nSession %d:", s);
407 cli_print(cli, "\tUser:\t\t%s", session[s].user[0] ? session[s].user : "none");
408 cli_print(cli, "\tCalling Num:\t%s", session[s].calling);
409 cli_print(cli, "\tCalled Num:\t%s", session[s].called);
410 cli_print(cli, "\tTunnel ID:\t%d", session[s].tunnel);
411 cli_print(cli, "\tPPP Phase:\t%s", ppp_phase(session[s].ppp.phase));
412 switch (session[s].ppp.phase)
413 {
414 case Establish:
415 cli_print(cli, "\t LCP state:\t%s", ppp_state(session[s].ppp.lcp));
416 break;
417
418 case Authenticate:
419 case Network:
420 cli_print(cli, "\t IPCP state:\t%s", ppp_state(session[s].ppp.ipcp));
421 cli_print(cli, "\t IPV6CP state:\t%s", ppp_state(session[s].ppp.ipv6cp));
422 cli_print(cli, "\t CCP state:\t%s", ppp_state(session[s].ppp.ccp));
423 }
424 cli_print(cli, "\tIP address:\t%s", fmtaddr(htonl(session[s].ip), 0));
425 cli_print(cli, "\tUnique SID:\t%u", session[s].unique_id);
426 cli_print(cli, "\tOpened:\t\t%u seconds", session[s].opened ? abs(time_now - session[s].opened) : 0);
427 cli_print(cli, "\tIdle time:\t%u seconds", session[s].last_packet ? abs(time_now - session[s].last_packet) : 0);
428 cli_print(cli, "\tNext Recv:\t%u", session[s].nr);
429 cli_print(cli, "\tNext Send:\t%u", session[s].ns);
430 cli_print(cli, "\tBytes In/Out:\t%u/%u", session[s].cout, session[s].cin);
431 cli_print(cli, "\tPkts In/Out:\t%u/%u", session[s].pout, session[s].pin);
432 cli_print(cli, "\tMRU:\t\t%d", session[s].mru);
433 cli_print(cli, "\tRx Speed:\t%u", session[s].rx_connect_speed);
434 cli_print(cli, "\tTx Speed:\t%u", session[s].tx_connect_speed);
435 if (session[s].filter_in && session[s].filter_in <= MAXFILTER)
436 cli_print(cli, "\tFilter in:\t%u (%s)", session[s].filter_in, ip_filters[session[s].filter_in - 1].name);
437 if (session[s].filter_out && session[s].filter_out <= MAXFILTER)
438 cli_print(cli, "\tFilter out:\t%u (%s)", session[s].filter_out, ip_filters[session[s].filter_out - 1].name);
439 if (session[s].snoop_ip && session[s].snoop_port)
440 cli_print(cli, "\tIntercepted:\t%s:%d", fmtaddr(session[s].snoop_ip, 0), session[s] .snoop_port);
441 else
442 cli_print(cli, "\tIntercepted:\tno");
443
444 cli_print(cli, "\tWalled Garden:\t%s", session[s].walled_garden ? "YES" : "no");
445 {
446 int t = (session[s].throttle_in || session[s].throttle_out);
447 cli_print(cli, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
448 t ? "YES" : "no", t ? " (" : "",
449 session[s].throttle_in, session[s].throttle_in ? "kbps" : t ? "-" : "",
450 t ? "/" : "",
451 session[s].throttle_out, session[s].throttle_out ? "kbps" : t ? "-" : "",
452 t ? ")" : "");
453 }
454
455 b_in = session[s].tbf_in;
456 b_out = session[s].tbf_out;
457 if (b_in || b_out)
458 cli_print(cli, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
459 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
460 "ByteSent", "PackSent", "PackDrop", "PackDelay");
461
462 if (b_in)
463 cli_print(cli, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
464 b_in,
465 (filter_list[b_in].next ? "*" : " "),
466 (b_in < 100 ? "\t" : ""),
467 filter_list[b_in].rate * 8,
468 filter_list[b_in].credit,
469 filter_list[b_in].queued,
470 filter_list[b_in].b_queued,
471 filter_list[b_in].p_queued,
472 filter_list[b_in].b_sent,
473 filter_list[b_in].p_sent,
474 filter_list[b_in].p_dropped,
475 filter_list[b_in].p_delayed);
476
477 if (b_out)
478 cli_print(cli, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
479 b_out,
480 (filter_list[b_out].next ? "*" : " "),
481 (b_out < 100 ? "\t" : ""),
482 filter_list[b_out].rate * 8,
483 filter_list[b_out].credit,
484 filter_list[b_out].queued,
485 filter_list[b_out].b_queued,
486 filter_list[b_out].p_queued,
487 filter_list[b_out].b_sent,
488 filter_list[b_out].p_sent,
489 filter_list[b_out].p_dropped,
490 filter_list[b_out].p_delayed);
491
492 }
493 return CLI_OK;
494 }
495
496 // Show Summary
497 cli_print(cli, "%5s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %-15s %s",
498 "SID",
499 "TID",
500 "Username",
501 "IP",
502 "I",
503 "T",
504 "G",
505 "6",
506 "opened",
507 "downloaded",
508 "uploaded",
509 "idle",
510 "LAC",
511 "CLI");
512
513 for (i = 1; i < MAXSESSION; i++)
514 {
515 if (!session[i].opened) continue;
516 cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %-15s %s",
517 i,
518 session[i].tunnel,
519 session[i].user[0] ? session[i].user : "*",
520 fmtaddr(htonl(session[i].ip), 0),
521 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
522 (session[i].throttle_in || session[i].throttle_out) ? "Y" : "N",
523 (session[i].walled_garden) ? "Y" : "N",
524 (session[i].ppp.ipv6cp == Opened) ? "Y" : "N",
525 abs(time_now - (unsigned long)session[i].opened),
526 (unsigned long)session[i].cout,
527 (unsigned long)session[i].cin,
528 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
529 fmtaddr(htonl(tunnel[ session[i].tunnel ].ip), 1),
530 session[i].calling[0] ? session[i].calling : "*");
531 }
532 return CLI_OK;
533 }
534
535 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc)
536 {
537 int i, x, show_all = 0;
538 char *states[] = {
539 "Free",
540 "Open",
541 "Closing",
542 "Opening",
543 };
544
545 if (CLI_HELP_REQUESTED)
546 {
547 if (argc > 1)
548 return cli_arg_help(cli, 1,
549 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
550 NULL);
551
552 return cli_arg_help(cli, 1,
553 "all", "Show all tunnels, including unused",
554 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
555 NULL);
556 }
557
558 time(&time_now);
559 if (argc > 0)
560 {
561 if (strcmp(argv[0], "all") == 0)
562 {
563 show_all = 1;
564 }
565 else
566 {
567 // Show individual tunnel
568 for (i = 0; i < argc; i++)
569 {
570 char s[65535] = {0};
571 unsigned int t;
572 t = atoi(argv[i]);
573 if (t <= 0 || t >= MAXTUNNEL)
574 {
575 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
576 continue;
577 }
578 cli_print(cli, "\r\nTunnel %d:", t);
579 cli_print(cli, "\tState:\t\t%s", states[tunnel[t].state]);
580 cli_print(cli, "\tHostname:\t%s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
581 cli_print(cli, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel[t].ip), 0));
582 cli_print(cli, "\tRemote Port:\t%d", tunnel[t].port);
583 cli_print(cli, "\tRx Window:\t%u", tunnel[t].window);
584 cli_print(cli, "\tNext Recv:\t%u", tunnel[t].nr);
585 cli_print(cli, "\tNext Send:\t%u", tunnel[t].ns);
586 cli_print(cli, "\tQueue Len:\t%u", tunnel[t].controlc);
587 cli_print(cli, "\tLast Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
588
589 for (x = 0; x < MAXSESSION; x++)
590 if (session[x].tunnel == t && session[x].opened && !session[x].die)
591 sprintf(s, "%s%u ", s, x);
592
593 cli_print(cli, "\tSessions:\t%s", s);
594 }
595 return CLI_OK;
596 }
597 }
598
599 // Show tunnel summary
600 cli_print(cli, "%4s %20s %20s %6s %s",
601 "TID",
602 "Hostname",
603 "IP",
604 "State",
605 "Sessions");
606
607 for (i = 1; i < MAXTUNNEL; i++)
608 {
609 int sessions = 0;
610 if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
611
612 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
613 cli_print(cli, "%4d %20s %20s %6s %6d",
614 i,
615 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
616 fmtaddr(htonl(tunnel[i].ip), 0),
617 states[tunnel[i].state],
618 sessions);
619 }
620
621 return CLI_OK;
622 }
623
624 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc)
625 {
626 char sid[32][8];
627 char *sargv[32];
628 int sargc = 0;
629 int i;
630
631 if (CLI_HELP_REQUESTED)
632 return cli_arg_help(cli, 1,
633 "USER", "Show details for specific username",
634 NULL);
635
636 for (i = 0; i < MAXSESSION; i++)
637 {
638 if (!session[i].opened) continue;
639 if (!session[i].user[0]) continue;
640 if (argc > 0)
641 {
642 int j;
643 for (j = 0; j < argc && sargc < 32; j++)
644 {
645 if (strcmp(argv[j], session[i].user) == 0)
646 {
647 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
648 sargv[sargc] = sid[sargc];
649 sargc++;
650 }
651 }
652
653 continue;
654 }
655
656 cli_print(cli, "%s", session[i].user);
657 }
658
659 if (sargc > 0)
660 return cmd_show_session(cli, "users", sargv, sargc);
661
662 return CLI_OK;
663 }
664
665 #ifdef STATISTICS
666 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
667 {
668 if (CLI_HELP_REQUESTED)
669 return CLI_HELP_NO_ARGS;
670
671 cli_print(cli, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
672 cli_print(cli, "%-10s %10u %10u %10u %10u", "RX",
673 GET_STAT(tun_rx_bytes),
674 GET_STAT(tun_rx_packets),
675 GET_STAT(tun_rx_errors),
676 GET_STAT(tun_rx_dropped));
677 cli_print(cli, "%-10s %10u %10u %10u", "TX",
678 GET_STAT(tun_tx_bytes),
679 GET_STAT(tun_tx_packets),
680 GET_STAT(tun_tx_errors));
681 cli_print(cli, "");
682
683 cli_print(cli, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
684 cli_print(cli, "%-10s %10u %10u %10u", "RX",
685 GET_STAT(tunnel_rx_bytes),
686 GET_STAT(tunnel_rx_packets),
687 GET_STAT(tunnel_rx_errors));
688 cli_print(cli, "%-10s %10u %10u %10u %10u", "TX",
689 GET_STAT(tunnel_tx_bytes),
690 GET_STAT(tunnel_tx_packets),
691 GET_STAT(tunnel_tx_errors),
692 GET_STAT(tunnel_retries));
693 cli_print(cli, "");
694
695 cli_print(cli, "%-30s%-10s", "Counter", "Value");
696 cli_print(cli, "-----------------------------------------");
697 cli_print(cli, "%-30s%u", "radius_retries", GET_STAT(radius_retries));
698 cli_print(cli, "%-30s%u", "arp_sent", GET_STAT(arp_sent));
699 cli_print(cli, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped));
700 cli_print(cli, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created));
701 cli_print(cli, "%-30s%u", "session_created", GET_STAT(session_created));
702 cli_print(cli, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout));
703 cli_print(cli, "%-30s%u", "session_timeout", GET_STAT(session_timeout));
704 cli_print(cli, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout));
705 cli_print(cli, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow));
706 cli_print(cli, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow));
707 cli_print(cli, "%-30s%u", "session_overflow", GET_STAT(session_overflow));
708 cli_print(cli, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated));
709 cli_print(cli, "%-30s%u", "ip_freed", GET_STAT(ip_freed));
710 cli_print(cli, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded));
711 cli_print(cli, "%-30s%u", "recv_forward", GET_STAT(recv_forward));
712 cli_print(cli, "%-30s%u", "select_called", GET_STAT(select_called));
713 cli_print(cli, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used));
714 cli_print(cli, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded));
715
716
717 #ifdef STAT_CALLS
718 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
719 cli_print(cli, "-----------------------------------------");
720 cli_print(cli, "%-30s%u", "call_processtun", GET_STAT(call_processtun));
721 cli_print(cli, "%-30s%u", "call_processipout", GET_STAT(call_processipout));
722 cli_print(cli, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out));
723 cli_print(cli, "%-30s%u", "call_processudp", GET_STAT(call_processudp));
724 cli_print(cli, "%-30s%u", "call_processpap", GET_STAT(call_processpap));
725 cli_print(cli, "%-30s%u", "call_processchap", GET_STAT(call_processchap));
726 cli_print(cli, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp));
727 cli_print(cli, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp));
728 cli_print(cli, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp));
729 cli_print(cli, "%-30s%u", "call_processipin", GET_STAT(call_processipin));
730 cli_print(cli, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in));
731 cli_print(cli, "%-30s%u", "call_processccp", GET_STAT(call_processccp));
732 cli_print(cli, "%-30s%u", "call_processrad", GET_STAT(call_processrad));
733 cli_print(cli, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp));
734 cli_print(cli, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp));
735 cli_print(cli, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap));
736 cli_print(cli, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip));
737 cli_print(cli, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6));
738 cli_print(cli, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
739 cli_print(cli, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend));
740 cli_print(cli, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill));
741 cli_print(cli, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
742 cli_print(cli, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill));
743 cli_print(cli, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
744 cli_print(cli, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup));
745 cli_print(cli, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address));
746 cli_print(cli, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address));
747 cli_print(cli, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
748 cli_print(cli, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend));
749 cli_print(cli, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry));
750 cli_print(cli, "%-30s%u", "call_random_data", GET_STAT(call_random_data));
751 #endif /* STAT_CALLS */
752
753 {
754 time_t l = GET_STAT(last_reset);
755 char *t = ctime(&l);
756 char *p = strchr(t, '\n');
757 if (p) *p = 0;
758
759 cli_print(cli, "");
760 cli_print(cli, "Last counter reset %s", t);
761 }
762
763 return CLI_OK;
764 }
765
766 static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, int argc)
767 {
768 if (CLI_HELP_REQUESTED)
769 return CLI_HELP_NO_ARGS;
770
771 memset(_statistics, 0, sizeof(struct Tstats));
772 SET_STAT(last_reset, time(NULL));
773
774 cli_print(cli, "Counters cleared");
775 return CLI_OK;
776 }
777 #endif /* STATISTICS */
778
779 static int cmd_show_version(struct cli_def *cli, char *command, char **argv, int argc)
780 {
781 int tag = 0;
782 int file = 0;
783 int i = 0;
784
785 if (CLI_HELP_REQUESTED)
786 return cli_arg_help(cli, 1,
787 "tag", "Include CVS release tag",
788 "file", "Include file versions",
789 NULL);
790
791 for (i = 0; i < argc; i++)
792 if (!strcmp(argv[i], "tag"))
793 tag++;
794 else if (!strcmp(argv[i], "file"))
795 file++;
796
797 cli_print(cli, "L2TPNS %s", VERSION);
798 if (tag)
799 {
800 char const *p = strchr(cvs_name, ':');
801 char const *e;
802 if (p)
803 {
804 p++;
805 while (isspace(*p))
806 p++;
807 }
808
809 if (!p || *p == '$')
810 p = "HEAD";
811
812 e = strpbrk(p, " \t$");
813 cli_print(cli, "Tag: %.*s", (int) (e ? e - p + 1 : strlen(p)), p);
814 }
815
816 if (file)
817 {
818 extern linked_list *loaded_plugins;
819 void *p;
820
821 cli_print(cli, "Files:");
822 cli_print(cli, " %s", cvs_id_arp);
823 #ifdef BGP
824 cli_print(cli, " %s", cvs_id_bgp);
825 #endif /* BGP */
826 cli_print(cli, " %s", cvs_id_cli);
827 cli_print(cli, " %s", cvs_id_cluster);
828 cli_print(cli, " %s", cvs_id_constants);
829 cli_print(cli, " %s", cvs_id_control);
830 cli_print(cli, " %s", cvs_id_icmp);
831 cli_print(cli, " %s", cvs_id_l2tpns);
832 cli_print(cli, " %s", cvs_id_ll);
833 cli_print(cli, " %s", cvs_id_ppp);
834 cli_print(cli, " %s", cvs_id_radius);
835 cli_print(cli, " %s", cvs_id_tbf);
836 cli_print(cli, " %s", cvs_id_util);
837
838 ll_reset(loaded_plugins);
839 while ((p = ll_next(loaded_plugins)))
840 {
841 char const **id = dlsym(p, "cvs_id");
842 if (id)
843 cli_print(cli, " %s", *id);
844 }
845 }
846
847 return CLI_OK;
848 }
849
850 static int cmd_show_pool(struct cli_def *cli, char *command, char **argv, int argc)
851 {
852 int i;
853 int used = 0, free = 0, show_all = 0;
854
855 if (!config->cluster_iam_master)
856 {
857 cli_print(cli, "Can't do this on a slave. Do it on %s",
858 fmtaddr(config->cluster_master_address, 0));
859
860 return CLI_OK;
861 }
862
863 if (CLI_HELP_REQUESTED)
864 {
865 if (argc > 1)
866 return cli_arg_help(cli, 1, NULL);
867
868 return cli_arg_help(cli, 1,
869 "all", "Show all pool addresses, including unused",
870 NULL);
871 }
872
873 if (argc > 0 && strcmp(argv[0], "all") == 0)
874 show_all = 1;
875
876 time(&time_now);
877 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
878 for (i = 0; i < MAXIPPOOL; i++)
879 {
880 if (!ip_address_pool[i].address) continue;
881 if (ip_address_pool[i].assigned)
882 {
883 cli_print(cli, "%-15s\tY %8d %s",
884 fmtaddr(htonl(ip_address_pool[i].address), 0),
885 ip_address_pool[i].session,
886 session[ip_address_pool[i].session].user);
887
888 used++;
889 }
890 else
891 {
892 if (ip_address_pool[i].last)
893 cli_print(cli, "%-15s\tN %8s [%s] %ds",
894 fmtaddr(htonl(ip_address_pool[i].address), 0), "",
895 ip_address_pool[i].user, (int) time_now - ip_address_pool[i].last);
896
897 else if (show_all)
898 cli_print(cli, "%-15s\tN", fmtaddr(htonl(ip_address_pool[i].address), 0));
899
900 free++;
901 }
902 }
903
904 if (!show_all)
905 cli_print(cli, "(Not displaying unused addresses)");
906
907 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
908 return CLI_OK;
909 }
910
911 static FILE *save_config_fh = 0;
912 static void print_save_config(struct cli_def *cli, char *string)
913 {
914 if (save_config_fh)
915 fprintf(save_config_fh, "%s\n", string);
916 }
917
918 static int cmd_write_memory(struct cli_def *cli, char *command, char **argv, int argc)
919 {
920 if (CLI_HELP_REQUESTED)
921 return CLI_HELP_NO_ARGS;
922
923 if ((save_config_fh = fopen(config->config_file, "w")))
924 {
925 cli_print(cli, "Writing configuration");
926 cli_print_callback(cli, print_save_config);
927 cmd_show_run(cli, command, argv, argc);
928 cli_print_callback(cli, NULL);
929 fclose(save_config_fh);
930 save_config_fh = 0;
931 }
932 else
933 {
934 cli_error(cli, "Error writing configuration: %s", strerror(errno));
935 }
936 return CLI_OK;
937 }
938
939 static char const *show_access_list_rule(int extended, ip_filter_rulet *rule);
940
941 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
942 {
943 int i;
944 char ipv6addr[INET6_ADDRSTRLEN];
945
946 if (CLI_HELP_REQUESTED)
947 return CLI_HELP_NO_ARGS;
948
949 cli_print(cli, "# Current configuration:");
950
951 for (i = 0; config_values[i].key; i++)
952 {
953 void *value = ((void *)config) + config_values[i].offset;
954 if (config_values[i].type == STRING)
955 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *) value);
956 else if (config_values[i].type == IPv4)
957 cli_print(cli, "set %s %s", config_values[i].key, fmtaddr(*(in_addr_t *) value, 0));
958 else if (config_values[i].type == IPv6)
959 cli_print(cli, "set %s %s", config_values[i].key, inet_ntop(AF_INET6, value, ipv6addr, INET6_ADDRSTRLEN));
960 else if (config_values[i].type == SHORT)
961 cli_print(cli, "set %s %hu", config_values[i].key, *(short *) value);
962 else if (config_values[i].type == BOOL)
963 cli_print(cli, "set %s %s", config_values[i].key, (*(int *) value) ? "yes" : "no");
964 else if (config_values[i].type == INT)
965 cli_print(cli, "set %s %d", config_values[i].key, *(int *) value);
966 else if (config_values[i].type == UNSIGNED_LONG)
967 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *) value);
968 else if (config_values[i].type == MAC)
969 cli_print(cli, "set %s %02x%02x.%02x%02x.%02x%02x", config_values[i].key,
970 *(unsigned short *) (value + 0),
971 *(unsigned short *) (value + 1),
972 *(unsigned short *) (value + 2),
973 *(unsigned short *) (value + 3),
974 *(unsigned short *) (value + 4),
975 *(unsigned short *) (value + 5));
976 }
977
978 cli_print(cli, "# Plugins");
979 for (i = 0; i < MAXPLUGINS; i++)
980 {
981 if (*config->plugins[i])
982 {
983 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
984 }
985 }
986
987 #ifdef BGP
988 if (config->as_number)
989 {
990 int k;
991 int h;
992
993 cli_print(cli, "# BGP");
994 cli_print(cli, "router bgp %u", config->as_number);
995 for (i = 0; i < BGP_NUM_PEERS; i++)
996 {
997 if (!config->neighbour[i].name[0])
998 continue;
999
1000 cli_print(cli, " neighbour %s remote-as %u", config->neighbour[i].name, config->neighbour[i].as);
1001
1002 k = config->neighbour[i].keepalive;
1003 h = config->neighbour[i].hold;
1004
1005 if (k == -1)
1006 {
1007 if (h == -1)
1008 continue;
1009
1010 k = BGP_KEEPALIVE_TIME;
1011 }
1012
1013 if (h == -1)
1014 h = BGP_HOLD_TIME;
1015
1016 cli_print(cli, " neighbour %s timers %d %d", config->neighbour[i].name, k, h);
1017 }
1018 }
1019 #endif
1020
1021 cli_print(cli, "# Filters");
1022 for (i = 0; i < MAXFILTER; i++)
1023 {
1024 ip_filter_rulet *rules;
1025 if (!*ip_filters[i].name)
1026 continue;
1027
1028 cli_print(cli, "ip access-list %s %s",
1029 ip_filters[i].extended ? "extended" : "standard",
1030 ip_filters[i].name);
1031
1032 rules = ip_filters[i].rules;
1033 while (rules->action)
1034 cli_print(cli, "%s", show_access_list_rule(ip_filters[i].extended, rules++));
1035 }
1036
1037 cli_print(cli, "# end");
1038 return CLI_OK;
1039 }
1040
1041 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc)
1042 {
1043 int i, free = 0, used = 0, show_all = 0;
1044 char *states[] = {
1045 "NULL",
1046 "CHAP",
1047 "AUTH",
1048 "IPCP",
1049 "START",
1050 "STOP",
1051 "INTRM",
1052 "WAIT",
1053 };
1054
1055 if (CLI_HELP_REQUESTED)
1056 {
1057 if (argc > 1)
1058 return cli_arg_help(cli, 1, NULL);
1059
1060 return cli_arg_help(cli, 1,
1061 "all", "Show all RADIUS sessions, including unused",
1062 NULL);
1063 }
1064
1065 cli_print(cli, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1066
1067 time(&time_now);
1068
1069 if (argc > 0 && strcmp(argv[0], "all") == 0)
1070 show_all = 1;
1071
1072 for (i = 1; i < MAXRADIUS; i++)
1073 {
1074 if (radius[i].state == RADIUSNULL)
1075 free++;
1076 else
1077 used++;
1078
1079 if (!show_all && radius[i].state == RADIUSNULL) continue;
1080
1081 cli_print(cli, "%6d%7d%5d%6s%9d%9u%4d",
1082 i,
1083 i >> RADIUS_SHIFT,
1084 i & RADIUS_MASK,
1085 states[radius[i].state],
1086 radius[i].session,
1087 radius[i].retry,
1088 radius[i].try);
1089 }
1090
1091 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
1092
1093 return CLI_OK;
1094 }
1095
1096 static int cmd_show_plugins(struct cli_def *cli, char *command, char **argv, int argc)
1097 {
1098 int i;
1099
1100 if (CLI_HELP_REQUESTED)
1101 return CLI_HELP_NO_ARGS;
1102
1103 cli_print(cli, "Plugins currently loaded:");
1104 for (i = 0; i < MAXPLUGINS; i++)
1105 if (*config->plugins[i])
1106 cli_print(cli, " %s", config->plugins[i]);
1107
1108 return CLI_OK;
1109 }
1110
1111 static int cmd_show_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1112 {
1113 int i;
1114
1115 if (CLI_HELP_REQUESTED)
1116 return CLI_HELP_NO_ARGS;
1117
1118 cli_print(cli, "%5s %4s %-32s %7s %6s %6s %6s",
1119 "SID",
1120 "TID",
1121 "Username",
1122 "Rate In",
1123 "Out",
1124 "TBFI",
1125 "TBFO");
1126
1127 for (i = 0; i < MAXSESSION; i++)
1128 {
1129 if (session[i].throttle_in || session[i].throttle_out)
1130 cli_print(cli, "%5d %4d %-32s %6d %6d %6d %6d",
1131 i,
1132 session[i].tunnel,
1133 session[i].user,
1134 session[i].throttle_in,
1135 session[i].throttle_out,
1136 session[i].tbf_in,
1137 session[i].tbf_out);
1138 }
1139
1140 return CLI_OK;
1141 }
1142
1143 static int cmd_show_banana(struct cli_def *cli, char *command, char **argv, int argc)
1144 {
1145 if (CLI_HELP_REQUESTED)
1146 return CLI_HELP_NO_ARGS;
1147
1148 cli_print(cli, " _\n"
1149 "//\\\n"
1150 "V \\\n"
1151 " \\ \\_\n"
1152 " \\,'.`-.\n"
1153 " |\\ `. `.\n"
1154 " ( \\ `. `-. _,.-:\\\n"
1155 " \\ \\ `. `-._ __..--' ,-';/\n"
1156 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1157 " `. `. `-._ __..--' ,' /\n"
1158 " `. `-_ ``--..'' _.-' ,'\n"
1159 " `-_ `-.___ __,--' ,'\n"
1160 " `-.__ `----\"\"\" __.-'\n"
1161 "hh `--..____..--'");
1162
1163 return CLI_OK;
1164 }
1165
1166 static int cmd_drop_user(struct cli_def *cli, char *command, char **argv, int argc)
1167 {
1168 int i;
1169 sessionidt s;
1170
1171 if (CLI_HELP_REQUESTED)
1172 return cli_arg_help(cli, argc > 1,
1173 "USER", "Username of session to drop", NULL);
1174
1175 if (!config->cluster_iam_master)
1176 {
1177 cli_error(cli, "Can't do this on a slave. Do it on %s",
1178 fmtaddr(config->cluster_master_address, 0));
1179
1180 return CLI_OK;
1181 }
1182
1183 if (!argc)
1184 {
1185 cli_error(cli, "Specify a user to drop");
1186 return CLI_OK;
1187 }
1188
1189 for (i = 0; i < argc; i++)
1190 {
1191 if (!(s = sessionbyuser(argv[i])))
1192 {
1193 cli_error(cli, "User %s is not connected", argv[i]);
1194 continue;
1195 }
1196
1197 if (session[s].ip && session[s].opened && !session[s].die)
1198 {
1199 cli_print(cli, "Dropping user %s", session[s].user);
1200 cli_session_actions[s].action |= CLI_SESS_KILL;
1201 }
1202 }
1203
1204 return CLI_OK;
1205 }
1206
1207 static int cmd_drop_tunnel(struct cli_def *cli, char *command, char **argv, int argc)
1208 {
1209 int i;
1210 tunnelidt t;
1211
1212 if (CLI_HELP_REQUESTED)
1213 return cli_arg_help(cli, argc > 1,
1214 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1215
1216 if (!config->cluster_iam_master)
1217 {
1218 cli_error(cli, "Can't do this on a slave. Do it on %s",
1219 fmtaddr(config->cluster_master_address, 0));
1220
1221 return CLI_OK;
1222 }
1223
1224 if (!argc)
1225 {
1226 cli_error(cli, "Specify a tunnel to drop");
1227 return CLI_OK;
1228 }
1229
1230 for (i = 0; i < argc; i++)
1231 {
1232 if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL))
1233 {
1234 cli_error(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1235 continue;
1236 }
1237
1238 if (!tunnel[t].ip)
1239 {
1240 cli_error(cli, "Tunnel %d is not connected", t);
1241 continue;
1242 }
1243
1244 if (tunnel[t].die)
1245 {
1246 cli_error(cli, "Tunnel %d is already being shut down", t);
1247 continue;
1248 }
1249
1250 cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname);
1251 cli_tunnel_actions[t].action |= CLI_TUN_KILL;
1252 }
1253
1254 return CLI_OK;
1255 }
1256
1257 static int cmd_drop_session(struct cli_def *cli, char *command, char **argv, int argc)
1258 {
1259 int i;
1260 sessionidt s;
1261
1262 if (CLI_HELP_REQUESTED)
1263 return cli_arg_help(cli, argc > 1,
1264 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1265
1266 if (!config->cluster_iam_master)
1267 {
1268 cli_error(cli, "Can't do this on a slave. Do it on %s",
1269 fmtaddr(config->cluster_master_address, 0));
1270
1271 return CLI_OK;
1272 }
1273
1274 if (!argc)
1275 {
1276 cli_error(cli, "Specify a session id to drop");
1277 return CLI_OK;
1278 }
1279
1280 for (i = 0; i < argc; i++)
1281 {
1282 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1283 {
1284 cli_error(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1285 continue;
1286 }
1287
1288 if (session[s].ip && session[s].opened && !session[s].die)
1289 {
1290 cli_print(cli, "Dropping session %d", s);
1291 cli_session_actions[s].action |= CLI_SESS_KILL;
1292 }
1293 else
1294 {
1295 cli_error(cli, "Session %d is not active.", s);
1296 }
1297 }
1298
1299 return CLI_OK;
1300 }
1301
1302 static int cmd_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1303 {
1304 in_addr_t ip;
1305 uint16_t port;
1306 sessionidt s;
1307
1308 if (CLI_HELP_REQUESTED)
1309 {
1310 switch (argc)
1311 {
1312 case 1:
1313 return cli_arg_help(cli, 0,
1314 "USER", "Username of session to snoop", NULL);
1315
1316 case 2:
1317 return cli_arg_help(cli, 0,
1318 "A.B.C.D", "IP address of snoop destination", NULL);
1319
1320 case 3:
1321 return cli_arg_help(cli, 0,
1322 "N", "Port of snoop destination", NULL);
1323
1324 case 4:
1325 if (!argv[3][1])
1326 return cli_arg_help(cli, 1, NULL);
1327
1328 default:
1329 return CLI_OK;
1330 }
1331 }
1332
1333 if (!config->cluster_iam_master)
1334 {
1335 cli_error(cli, "Can't do this on a slave. Do it on %s",
1336 fmtaddr(config->cluster_master_address, 0));
1337
1338 return CLI_OK;
1339 }
1340
1341 if (argc < 3)
1342 {
1343 cli_error(cli, "Specify username, ip and port");
1344 return CLI_OK;
1345 }
1346
1347 if (!(s = sessionbyuser(argv[0])))
1348 {
1349 cli_error(cli, "User %s is not connected", argv[0]);
1350 return CLI_OK;
1351 }
1352
1353 ip = inet_addr(argv[1]);
1354 if (!ip || ip == INADDR_NONE)
1355 {
1356 cli_error(cli, "Cannot parse IP \"%s\"", argv[1]);
1357 return CLI_OK;
1358 }
1359
1360 port = atoi(argv[2]);
1361 if (!port)
1362 {
1363 cli_error(cli, "Invalid port %s", argv[2]);
1364 return CLI_OK;
1365 }
1366
1367 cli_print(cli, "Snooping user %s to %s:%d", argv[0], fmtaddr(ip, 0), port);
1368 cli_session_actions[s].snoop_ip = ip;
1369 cli_session_actions[s].snoop_port = port;
1370 cli_session_actions[s].action |= CLI_SESS_SNOOP;
1371
1372 return CLI_OK;
1373 }
1374
1375 static int cmd_no_snoop(struct cli_def *cli, char *command, char **argv, int argc)
1376 {
1377 int i;
1378 sessionidt s;
1379
1380 if (CLI_HELP_REQUESTED)
1381 return cli_arg_help(cli, argc > 1,
1382 "USER", "Username of session to unsnoop", NULL);
1383
1384 if (!config->cluster_iam_master)
1385 {
1386 cli_error(cli, "Can't do this on a slave. Do it on %s",
1387 fmtaddr(config->cluster_master_address, 0));
1388
1389 return CLI_OK;
1390 }
1391
1392 if (!argc)
1393 {
1394 cli_error(cli, "Specify a user to unsnoop");
1395 return CLI_OK;
1396 }
1397
1398 for (i = 0; i < argc; i++)
1399 {
1400 if (!(s = sessionbyuser(argv[i])))
1401 {
1402 cli_error(cli, "User %s is not connected", argv[i]);
1403 continue;
1404 }
1405
1406 cli_print(cli, "Not snooping user %s", argv[i]);
1407 cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
1408 }
1409
1410 return CLI_OK;
1411 }
1412
1413 static int cmd_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1414 {
1415 int rate_in = 0;
1416 int rate_out = 0;
1417 sessionidt s;
1418
1419 /*
1420 throttle USER - throttle in/out to default rate
1421 throttle USER RATE - throttle in/out to default rate
1422 throttle USER in RATE - throttle input only
1423 throttle USER out RATE - throttle output only
1424 throttle USER in RATE out RATE - throttle both
1425 */
1426
1427 if (CLI_HELP_REQUESTED)
1428 {
1429 switch (argc)
1430 {
1431 case 1:
1432 return cli_arg_help(cli, 0,
1433 "USER", "Username of session to throttle", NULL);
1434
1435 case 2:
1436 return cli_arg_help(cli, 1,
1437 "RATE", "Rate in kbps (in and out)",
1438 "in", "Select incoming rate",
1439 "out", "Select outgoing rate", NULL);
1440
1441 case 4:
1442 return cli_arg_help(cli, 1,
1443 "in", "Select incoming rate",
1444 "out", "Select outgoing rate", NULL);
1445
1446 case 3:
1447 if (isdigit(argv[1][0]))
1448 return cli_arg_help(cli, 1, NULL);
1449
1450 case 5:
1451 return cli_arg_help(cli, 0, "RATE", "Rate in kbps", NULL);
1452
1453 default:
1454 return cli_arg_help(cli, argc > 1, NULL);
1455 }
1456 }
1457
1458 if (!config->cluster_iam_master)
1459 {
1460 cli_error(cli, "Can't do this on a slave. Do it on %s",
1461 fmtaddr(config->cluster_master_address, 0));
1462
1463 return CLI_OK;
1464 }
1465
1466 if (argc == 0)
1467 {
1468 cli_error(cli, "Specify a user to throttle");
1469 return CLI_OK;
1470 }
1471
1472 if (!(s = sessionbyuser(argv[0])))
1473 {
1474 cli_error(cli, "User %s is not connected", argv[0]);
1475 return CLI_OK;
1476 }
1477
1478 if (argc == 1)
1479 {
1480 rate_in = rate_out = config->rl_rate;
1481 }
1482 else if (argc == 2)
1483 {
1484 rate_in = rate_out = atoi(argv[1]);
1485 if (rate_in < 1)
1486 {
1487 cli_error(cli, "Invalid rate \"%s\"", argv[1]);
1488 return CLI_OK;
1489 }
1490 }
1491 else if (argc == 3 || argc == 5)
1492 {
1493 int i;
1494 for (i = 1; i < argc - 1; i += 2)
1495 {
1496 int r = 0;
1497 if (MATCH("in", argv[i]))
1498 r = rate_in = atoi(argv[i+1]);
1499 else if (MATCH("out", argv[i]))
1500 r = rate_out = atoi(argv[i+1]);
1501
1502 if (r < 1)
1503 {
1504 cli_error(cli, "Invalid rate specification \"%s %s\"", argv[i], argv[i+1]);
1505 return CLI_OK;
1506 }
1507 }
1508 }
1509 else
1510 {
1511 cli_error(cli, "Invalid arguments");
1512 return CLI_OK;
1513 }
1514
1515 if ((rate_in && session[s].throttle_in) || (rate_out && session[s].throttle_out))
1516 {
1517 cli_error(cli, "User %s already throttled, unthrottle first", argv[0]);
1518 return CLI_OK;
1519 }
1520
1521 cli_session_actions[s].throttle_in = cli_session_actions[s].throttle_out = -1;
1522 if (rate_in && session[s].throttle_in != rate_in)
1523 cli_session_actions[s].throttle_in = rate_in;
1524
1525 if (rate_out && session[s].throttle_out != rate_out)
1526 cli_session_actions[s].throttle_out = rate_out;
1527
1528 if (cli_session_actions[s].throttle_in == -1 &&
1529 cli_session_actions[s].throttle_out == -1)
1530 {
1531 cli_error(cli, "User %s already throttled at this rate", argv[0]);
1532 return CLI_OK;
1533 }
1534
1535 cli_print(cli, "Throttling user %s", argv[0]);
1536 cli_session_actions[s].action |= CLI_SESS_THROTTLE;
1537
1538 return CLI_OK;
1539 }
1540
1541 static int cmd_no_throttle(struct cli_def *cli, char *command, char **argv, int argc)
1542 {
1543 int i;
1544 sessionidt s;
1545
1546 if (CLI_HELP_REQUESTED)
1547 return cli_arg_help(cli, argc > 1,
1548 "USER", "Username of session to unthrottle", NULL);
1549
1550 if (!config->cluster_iam_master)
1551 {
1552 cli_error(cli, "Can't do this on a slave. Do it on %s",
1553 fmtaddr(config->cluster_master_address, 0));
1554
1555 return CLI_OK;
1556 }
1557
1558 if (!argc)
1559 {
1560 cli_error(cli, "Specify a user to unthrottle");
1561 return CLI_OK;
1562 }
1563
1564 for (i = 0; i < argc; i++)
1565 {
1566 if (!(s = sessionbyuser(argv[i])))
1567 {
1568 cli_error(cli, "User %s is not connected", argv[i]);
1569 continue;
1570 }
1571
1572 if (session[s].throttle_in || session[s].throttle_out)
1573 {
1574 cli_print(cli, "Unthrottling user %s", argv[i]);
1575 cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
1576 }
1577 else
1578 {
1579 cli_error(cli, "User %s not throttled", argv[i]);
1580 }
1581 }
1582
1583 return CLI_OK;
1584 }
1585
1586 static int cmd_debug(struct cli_def *cli, char *command, char **argv, int argc)
1587 {
1588 int i;
1589
1590 if (CLI_HELP_REQUESTED)
1591 return cli_arg_help(cli, 1,
1592 "all", "Enable debugging for all except \"data\"",
1593 "critical", "", // FIXME: add descriptions
1594 "error", "",
1595 "warning", "",
1596 "info", "",
1597 "calls", "",
1598 "data", "",
1599 NULL);
1600
1601 if (!argc)
1602 {
1603 char *p = (char *) &debug_flags;
1604 for (i = 0; i < sizeof(debug_flags); i++)
1605 {
1606 if (p[i])
1607 {
1608 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1609 (debug_flags.critical) ? " critical" : "",
1610 (debug_flags.error) ? " error" : "",
1611 (debug_flags.warning) ? " warning" : "",
1612 (debug_flags.info) ? " info" : "",
1613 (debug_flags.calls) ? " calls" : "",
1614 (debug_flags.data) ? " data" : "");
1615
1616 return CLI_OK;
1617 }
1618 }
1619
1620 cli_print(cli, "Debugging off");
1621 return CLI_OK;
1622 }
1623
1624 for (i = 0; i < argc; i++)
1625 {
1626 int len = strlen(argv[i]);
1627
1628 if (argv[i][0] == 'c' && len < 2)
1629 len = 2; /* distinguish [cr]itical from [ca]lls */
1630
1631 if (!strncmp("critical", argv[i], len)) { debug_flags.critical = 1; continue; }
1632 if (!strncmp("error", argv[i], len)) { debug_flags.error = 1; continue; }
1633 if (!strncmp("warning", argv[i], len)) { debug_flags.warning = 1; continue; }
1634 if (!strncmp("info", argv[i], len)) { debug_flags.info = 1; continue; }
1635 if (!strncmp("calls", argv[i], len)) { debug_flags.calls = 1; continue; }
1636 if (!strncmp("data", argv[i], len)) { debug_flags.data = 1; continue; }
1637 if (!strncmp("all", argv[i], len))
1638 {
1639 memset(&debug_flags, 1, sizeof(debug_flags));
1640 debug_flags.data = 0;
1641 continue;
1642 }
1643
1644 cli_error(cli, "Invalid debugging flag \"%s\"", argv[i]);
1645 }
1646
1647 return CLI_OK;
1648 }
1649
1650 static int cmd_no_debug(struct cli_def *cli, char *command, char **argv, int argc)
1651 {
1652 int i;
1653
1654 if (CLI_HELP_REQUESTED)
1655 return cli_arg_help(cli, 1,
1656 "all", "Disable all debugging",
1657 "critical", "", // FIXME: add descriptions
1658 "error", "",
1659 "warning", "",
1660 "info", "",
1661 "calls", "",
1662 "data", "",
1663 NULL);
1664
1665 if (!argc)
1666 {
1667 memset(&debug_flags, 0, sizeof(debug_flags));
1668 return CLI_OK;
1669 }
1670
1671 for (i = 0; i < argc; i++)
1672 {
1673 int len = strlen(argv[i]);
1674
1675 if (argv[i][0] == 'c' && len < 2)
1676 len = 2; /* distinguish [cr]itical from [ca]lls */
1677
1678 if (!strncmp("critical", argv[i], len)) { debug_flags.critical = 0; continue; }
1679 if (!strncmp("error", argv[i], len)) { debug_flags.error = 0; continue; }
1680 if (!strncmp("warning", argv[i], len)) { debug_flags.warning = 0; continue; }
1681 if (!strncmp("info", argv[i], len)) { debug_flags.info = 0; continue; }
1682 if (!strncmp("calls", argv[i], len)) { debug_flags.calls = 0; continue; }
1683 if (!strncmp("data", argv[i], len)) { debug_flags.data = 0; continue; }
1684 if (!strncmp("all", argv[i], len))
1685 {
1686 memset(&debug_flags, 0, sizeof(debug_flags));
1687 continue;
1688 }
1689
1690 cli_error(cli, "Invalid debugging flag \"%s\"", argv[i]);
1691 }
1692
1693 return CLI_OK;
1694 }
1695
1696 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1697 {
1698 int i, firstfree = 0;
1699
1700 if (CLI_HELP_REQUESTED)
1701 return cli_arg_help(cli, argc > 1,
1702 "PLUGIN", "Name of plugin to load", NULL);
1703
1704 if (argc != 1)
1705 {
1706 cli_error(cli, "Specify a plugin to load");
1707 return CLI_OK;
1708 }
1709
1710 for (i = 0; i < MAXPLUGINS; i++)
1711 {
1712 if (!*config->plugins[i] && !firstfree)
1713 firstfree = i;
1714 if (strcmp(config->plugins[i], argv[0]) == 0)
1715 {
1716 cli_error(cli, "Plugin is already loaded");
1717 return CLI_OK;
1718 }
1719 }
1720
1721 if (firstfree)
1722 {
1723 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1724 config->reload_config = 1;
1725 cli_print(cli, "Loading plugin %s", argv[0]);
1726 }
1727
1728 return CLI_OK;
1729 }
1730
1731 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc)
1732 {
1733 int i;
1734
1735 if (CLI_HELP_REQUESTED)
1736 return cli_arg_help(cli, argc > 1,
1737 "PLUGIN", "Name of plugin to unload", NULL);
1738
1739 if (argc != 1)
1740 {
1741 cli_error(cli, "Specify a plugin to remove");
1742 return CLI_OK;
1743 }
1744
1745 for (i = 0; i < MAXPLUGINS; i++)
1746 {
1747 if (strcmp(config->plugins[i], argv[0]) == 0)
1748 {
1749 config->reload_config = 1;
1750 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1751 return CLI_OK;
1752 }
1753 }
1754
1755 cli_error(cli, "Plugin is not loaded");
1756 return CLI_OK;
1757 }
1758
1759 static char *duration(time_t secs)
1760 {
1761 static char *buf = NULL;
1762 int p = 0;
1763
1764 if (!buf) buf = calloc(64, 1);
1765
1766 if (secs >= 86400)
1767 {
1768 int days = secs / 86400;
1769 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1770 secs %= 86400;
1771 }
1772
1773 if (secs >= 3600)
1774 {
1775 int mins = secs / 60;
1776 int hrs = mins / 60;
1777
1778 mins %= 60;
1779 sprintf(buf + p, "%d:%02d", hrs, mins);
1780 }
1781 else if (secs >= 60)
1782 {
1783 int mins = secs / 60;
1784 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1785 }
1786 else
1787 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1788
1789 return buf;
1790 }
1791
1792 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
1793 {
1794 FILE *fh;
1795 char buf[100], *p = buf, *loads[3];
1796 int i, num_sessions = 0;
1797
1798 if (CLI_HELP_REQUESTED)
1799 return CLI_HELP_NO_ARGS;
1800
1801 fh = fopen("/proc/loadavg", "r");
1802 fgets(buf, 100, fh);
1803 fclose(fh);
1804
1805 for (i = 0; i < 3; i++)
1806 loads[i] = strdup(strsep(&p, " "));
1807
1808 time(&time_now);
1809 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1810
1811 for (i = 1; i < MAXSESSION; i++)
1812 if (session[i].opened) num_sessions++;
1813
1814 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1815 buf,
1816 duration(time_now - config->start_time),
1817 num_sessions,
1818 loads[0], loads[1], loads[2]
1819 );
1820 for (i = 0; i < 3; i++)
1821 if (loads[i]) free(loads[i]);
1822
1823 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1824
1825 return CLI_OK;
1826 }
1827
1828 static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
1829 {
1830 int i;
1831
1832 if (CLI_HELP_REQUESTED)
1833 {
1834 switch (argc)
1835 {
1836 case 1:
1837 {
1838 int len = strlen(argv[0])-1;
1839 for (i = 0; config_values[i].key; i++)
1840 if (!len || !strncmp(config_values[i].key, argv[0], len))
1841 cli_error(cli, " %s", config_values[i].key);
1842 }
1843
1844 return CLI_OK;
1845
1846 case 2:
1847 return cli_arg_help(cli, 0,
1848 "VALUE", "Value for variable", NULL);
1849
1850 case 3:
1851 if (!argv[2][1])
1852 return cli_arg_help(cli, 1, NULL);
1853
1854 default:
1855 return CLI_OK;
1856 }
1857 }
1858
1859 if (argc != 2)
1860 {
1861 cli_error(cli, "Specify variable and value");
1862 return CLI_OK;
1863 }
1864
1865 for (i = 0; config_values[i].key; i++)
1866 {
1867 void *value = ((void *) config) + config_values[i].offset;
1868 if (strcmp(config_values[i].key, argv[0]) == 0)
1869 {
1870 // Found a value to set
1871 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1872 switch (config_values[i].type)
1873 {
1874 case STRING:
1875 snprintf((char *) value, config_values[i].size, "%s", argv[1]);
1876 break;
1877 case INT:
1878 *(int *) value = atoi(argv[1]);
1879 break;
1880 case UNSIGNED_LONG:
1881 *(unsigned long *) value = atol(argv[1]);
1882 break;
1883 case SHORT:
1884 *(short *) value = atoi(argv[1]);
1885 break;
1886 case IPv4:
1887 *(in_addr_t *) value = inet_addr(argv[1]);
1888 break;
1889 case IPv6:
1890 inet_pton(AF_INET6, argv[1], value);
1891 break;
1892 case MAC:
1893 parsemac(argv[1], (char *)value);
1894 break;
1895 case BOOL:
1896 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1897 *(int *) value = 1;
1898 else
1899 *(int *) value = 0;
1900 break;
1901 default:
1902 cli_error(cli, "Unknown variable type");
1903 break;
1904 }
1905 config->reload_config = 1;
1906 return CLI_OK;
1907 }
1908 }
1909
1910 cli_error(cli, "Unknown variable \"%s\"", argv[0]);
1911 return CLI_OK;
1912 }
1913
1914 int regular_stuff(struct cli_def *cli)
1915 {
1916 #ifdef RINGBUFFER
1917 int out = 0;
1918 int i;
1919
1920 for (i = debug_rb_tail; i != ringbuffer->tail; i = (i + 1) % RINGBUFFER_SIZE)
1921 {
1922 char *m = ringbuffer->buffer[i].message;
1923 char *p;
1924 int show = 0;
1925
1926 if (!*m) continue;
1927
1928 switch (ringbuffer->buffer[i].level)
1929 {
1930 case 0: show = debug_flags.critical; break;
1931 case 1: show = debug_flags.error; break;
1932 case 2: show = debug_flags.warning; break;
1933 case 3: show = debug_flags.info; break;
1934 case 4: show = debug_flags.calls; break;
1935 case 5: show = debug_flags.data; break;
1936 }
1937
1938 if (!show) continue;
1939
1940 if (!(p = strchr(m, '\n')))
1941 p = m + strlen(p);
1942
1943 cli_error(cli, "\r%s-%u-%u %.*s",
1944 debug_levels[(int)ringbuffer->buffer[i].level],
1945 ringbuffer->buffer[i].tunnel,
1946 ringbuffer->buffer[i].session,
1947 (int) (p - m), m);
1948
1949 out++;
1950 }
1951
1952 debug_rb_tail = ringbuffer->tail;
1953 if (out)
1954 cli_reprompt(cli);
1955 #endif
1956 return CLI_OK;
1957 }
1958
1959 #ifdef BGP
1960 static int cmd_router_bgp(struct cli_def *cli, char *command, char **argv, int argc)
1961 {
1962 int as;
1963
1964 if (CLI_HELP_REQUESTED)
1965 return cli_arg_help(cli, argc > 1,
1966 "<1-65535>", "Autonomous system number", NULL);
1967
1968 if (argc != 1 || (as = atoi(argv[0])) < 1 || as > 65535)
1969 {
1970 cli_error(cli, "Invalid autonomous system number");
1971 return CLI_OK;
1972 }
1973
1974 if (bgp_configured && as != config->as_number)
1975 {
1976 cli_error(cli, "Can't change local AS on a running system");
1977 return CLI_OK;
1978 }
1979
1980 config->as_number = as;
1981 cli_set_configmode(cli, MODE_CONFIG_BGP, "router");
1982
1983 return CLI_OK;
1984 }
1985
1986 static int find_bgp_neighbour(char const *name)
1987 {
1988 int i;
1989 int new = -1;
1990 struct hostent *h;
1991 in_addr_t addrs[4] = { 0 };
1992 char **a;
1993
1994 if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET)
1995 return -2;
1996
1997 for (i = 0; i < sizeof(addrs) / sizeof(*addrs) && h->h_addr_list[i]; i++)
1998 memcpy(&addrs[i], h->h_addr_list[i], sizeof(*addrs));
1999
2000 for (i = 0; i < BGP_NUM_PEERS; i++)
2001 {
2002 if (!config->neighbour[i].name[0])
2003 {
2004 if (new == -1) new = i;
2005 continue;
2006 }
2007
2008 if (!strcmp(name, config->neighbour[i].name))
2009 return i;
2010
2011 if (!(h = gethostbyname(config->neighbour[i].name)) || h->h_addrtype != AF_INET)
2012 continue;
2013
2014 for (a = h->h_addr_list; *a; a++)
2015 {
2016 int j;
2017 for (j = 0; j < sizeof(addrs) / sizeof(*addrs) && addrs[j]; j++)
2018 if (!memcmp(&addrs[j], *a, sizeof(*addrs)))
2019 return i;
2020 }
2021 }
2022
2023 return new;
2024 }
2025
2026 static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **argv, int argc)
2027 {
2028 int i;
2029 int keepalive;
2030 int hold;
2031
2032 if (CLI_HELP_REQUESTED)
2033 {
2034 switch (argc)
2035 {
2036 case 1:
2037 return cli_arg_help(cli, 0,
2038 "A.B.C.D", "BGP neighbour address",
2039 "NAME", "BGP neighbour name",
2040 NULL);
2041
2042 case 2:
2043 return cli_arg_help(cli, 0,
2044 "remote-as", "Set remote autonomous system number",
2045 "timers", "Set timers",
2046 NULL);
2047
2048 default:
2049 if (MATCH("remote-as", argv[1]))
2050 return cli_arg_help(cli, argv[2][1], "<1-65535>", "Autonomous system number", NULL);
2051
2052 if (MATCH("timers", argv[1]))
2053 {
2054 if (argc == 3)
2055 return cli_arg_help(cli, 0, "<1-65535>", "Keepalive time", NULL);
2056
2057 if (argc == 4)
2058 return cli_arg_help(cli, argv[3][1], "<3-65535>", "Hold time", NULL);
2059
2060 if (argc == 5 && !argv[4][1])
2061 return cli_arg_help(cli, 1, NULL);
2062 }
2063
2064 return CLI_OK;
2065 }
2066 }
2067
2068 if (argc < 3)
2069 {
2070 cli_error(cli, "Invalid arguments");
2071 return CLI_OK;
2072 }
2073
2074 if ((i = find_bgp_neighbour(argv[0])) == -2)
2075 {
2076 cli_error(cli, "Invalid neighbour");
2077 return CLI_OK;
2078 }
2079
2080 if (i == -1)
2081 {
2082 cli_error(cli, "Too many neighbours (max %d)", BGP_NUM_PEERS);
2083 return CLI_OK;
2084 }
2085
2086 if (MATCH("remote-as", argv[1]))
2087 {
2088 int as = atoi(argv[2]);
2089 if (as < 0 || as > 65535)
2090 {
2091 cli_error(cli, "Invalid autonomous system number");
2092 return CLI_OK;
2093 }
2094
2095 if (!config->neighbour[i].name[0])
2096 {
2097 snprintf(config->neighbour[i].name, sizeof(config->neighbour[i].name), "%s", argv[0]);
2098 config->neighbour[i].keepalive = -1;
2099 config->neighbour[i].hold = -1;
2100 }
2101
2102 config->neighbour[i].as = as;
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, sizeof(*rule)))
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 (%d 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 // Convert a string in the form of abcd.ef12.3456 into char[6]
3082 void parsemac(char *string, char mac[6])
3083 {
3084 if (sscanf(string, "%02x%02x.%02x%02x.%02x%02x", (unsigned int *)&mac[0], (unsigned int *)&mac[1], (unsigned int *)&mac[2], (unsigned int *)&mac[3], (unsigned int *)&mac[4], (unsigned int *)&mac[5]) == 6)
3085 return;
3086 if (sscanf(string, "%02x%02x:%02x%02x:%02x%02x", (unsigned int *)&mac[0], (unsigned int *)&mac[1], (unsigned int *)&mac[2], (unsigned int *)&mac[3], (unsigned int *)&mac[4], (unsigned int *)&mac[5]) == 6)
3087 return;
3088 memset(mac, 0, 6);
3089 }