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